hdu2873 Bomb Game
来源:互联网 发布:英语软件哪个比较好 编辑:程序博客网 时间:2024/06/07 06:19
Bomb Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 440 Accepted Submission(s): 206
Problem Description
John and Jack, two mathematicians, created a game called “Bomb Game” at spared time. This game is played on an n*m chessboard. A pair of integers (p, q) represents the grid at row p, column q. Some bombs were placed on the chessboard at the beginning. Every round, a player can choose to explode a bomb located at (p, q), and the exploded bomb will disappear. Furthermore:
1.If p>1 and q>1, the bomb will split up into two bombs located at (u, q) and (p, v), u<p, v<q, u and v are chosen by the player.
2.If p=1 and q>1, one new bomb will be produced, located at (p, v), v<q, v can be chosen freely by the player.
3.If q=1 and p>1, one new bomb will be produced, located at (u, q), u<p, u can be chosen freely by the player.
If two bombs located at the same position or a bomb located at (1, 1), they will be exploded automatically without producing new bombs.
Two players play in turn, until one player cannot explode the bombs and loses the game.
John always plays first.
Now, we’ll give you an initial situation, and you should tell us who will win at last. Assume John and Jack are smart enough, and they always do the best choice.
1.If p>1 and q>1, the bomb will split up into two bombs located at (u, q) and (p, v), u<p, v<q, u and v are chosen by the player.
2.If p=1 and q>1, one new bomb will be produced, located at (p, v), v<q, v can be chosen freely by the player.
3.If q=1 and p>1, one new bomb will be produced, located at (u, q), u<p, u can be chosen freely by the player.
If two bombs located at the same position or a bomb located at (1, 1), they will be exploded automatically without producing new bombs.
Two players play in turn, until one player cannot explode the bombs and loses the game.
John always plays first.
Now, we’ll give you an initial situation, and you should tell us who will win at last. Assume John and Jack are smart enough, and they always do the best choice.
Input
There are several test cases, each one begins with two integers n and m, 0<n, m<=50, represents the number of rows and columns. Following by an n*m grid, describing the initial situation, ‘#’ indicates bomb.
The input is terminated by n=m=0.
The input is terminated by n=m=0.
Output
For each test case, output one line, the name of the winner.
Sample Input
2 2.#..2 2.#.#0 0
Sample Output
JohnJack可以直接根据sg 函数,直接打表求出sg的值 ,这样就可以转化成一般的nim游戏了!sg函数的定义:
首先定义mex(minimal excludant)运算,这是施加于一个集合的运算,表示最小的不属于这个集合的非负整数。例如mex{0,1,2,4}=3、mex{2,3,5}=0、mex{}=0。
对于一个给定的有向无环图,定义关于图的每个顶点的SG函数如下:sg(x)=mex{ sg(y) | y是x的后继 },即一个节点的sg函数值等于其所有后继结点的sg函数值集合的mex
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;#define mem(a,b) memset(a,b,sizeof(a))#define M 55int pri[M][M],map[10000];int getsg(int a,int b){ int i,j; mem(map,0); for(i=0;i<a;i++) for(j=0;j<b;j++){ map[pri[a][j]^pri[i][b]]=1; } for(i=0;;i++){ if(!map[i]) return i; }}int main(){ int n,m,ans,i,j;char c; mem(pri,-1); for(i=0;i<M;i++) pri[0][i]=pri[i][0]=i; for(i=1;i<M;i++) for(j=1;j<M;j++){ pri[i][j]=getsg(i,j); } while(scanf("%d%d",&n,&m)!=EOF&&n+m){ getchar(); for(ans=0,i=0;i<n;i++){ for(j=0;j<m;j++){ c=getchar(); if(c=='#'){ ans^=pri[i][j]; } } getchar(); } if(ans)printf("John\n"); else printf("Jack\n"); } return 0;}
- hdu2873 Bomb Game-----sg
- hdu2873 Bomb Game
- hdu2873 Bomb Game 二维SG博弈
- Bomb Game
- hdoj 3622 Bomb Game
- hdu 2873 Bomb Game
- hdu 2873 Bomb Game
- hdu 3622 bomb game
- hdu 3622 Bomb Game
- hdu 3622 Bomb Game
- HDOJ 3622 Bomb Game
- HDU3622 Bomb Game
- hdu 3622 Bomb Game
- HDU 2873 Bomb Game
- HDU-3622 Bomb Game
- HDU-3622 Bomb Game
- HDU-2873 Bomb Game
- hdu 2873 Bomb Game
- C++11标准库之Type Traits简介
- ContentProvider/ContentResolver和Uri详解
- 二叉树相关问题
- OpenCV:概述、结构和内容
- HDOJ 1540 && POJ 2892 —— 线段树
- hdu2873 Bomb Game
- Ubuntu下如何将普通用户提升到root权限
- 深信服笔试题
- 应用程序正常初始化(0xc0000135)失败
- C++虚基类(virtual base class)
- 关于重构手机APP页面的若干技术问题
- 设计模式背后的思想
- 桌面文件夹图标变黑了 & 解决办法!~~
- Cocos2d-x 内存管理剖析(1)