Bomb Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 569 Accepted Submission(s): 265
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
John Jack
题意:在n*m的格子上有一些炸弹,爆炸一个炸弹后,会在同行的左方和同列的上方新增两个炸弹,如果一个格子中有两个炸弹则爆炸不留下炸弹。最后不能爆炸炸弹的输,问先手胜负。
思路:和翻硬币的原理是一样的,因为数据比较小,可以枚举出所有点上的sg值。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int T,t,n,m;
int sg[60][60];
char s[60][60];
int vis[10000];
void init()
{
int i,j,k,x,y;
sg[1][1]=0;
for(i=2;i<=50;i++)
sg[1][i]=sg[i][1]=i-1;
for(i=2;i<=50;i++)
for(j=2;j<=50;j++)
{
t++;
for(x=1;x<i;x++)
for(y=1;y<j;y++)
vis[sg[i][y]^sg[x][j]]=t;
for(k=0;k<5000;k++)
if(vis[k]!=t)
{
sg[i][j]=k;
break;
}
}
}
int main()
{
int i,j,k,S;
init();
while(~scanf("%d%d",&n,&m) && n+m)
{
S=0;
for(i=1;i<=n;i++)
scanf("%s",s[i]+1);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(s[i][j]=='#')
S^=sg[i][j];
if(S==0)
printf("Jack\n");
else
printf("John\n");
}
}