题目链接:点这里。
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.
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.
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
【题意】
给你一张图,图中会有很多炸弹,然后两个人相互游戏。每次可以选择一个炸弹,如果炸弹没有贴边,也就是到达图的边界,那么就可以选择两个右下角同行或者同列的位置,然后将这个炸弹分成两个。对于贴边的炸弹,当然只能分成一个。然后如果同一个位置有两个炸弹,那么这两个炸弹会自动爆炸。然后如果炸弹到了(右下角)也会自动爆炸。然后问你是先手必胜还是后手必胜。
【分析】
因为数据量很小,我们直接求出各个点的SG值。然后求出nim和就行了。
【代码】
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<sstream>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS_1(a) memset(a,-1,sizeof(a))
#define MSinf(a) memset(a,0x3f,sizeof(a))
#define sin1(a) scanf("%d",&(a))
#define sin2(a,b) scanf("%d%d",&(a),&(b))
#define sll1(a) scanf("%lld",&(a))
#define sll2(a,b) scanf("%lld%lld",&(a),&(b))
#define sdo1(a) scanf("%lf",&(a))
#define sdo2(a,b) scanf("%lf%lf",&(a),&(b))
#define inf 0x3f3f3f3f
//#define lson i<<1,l,mid
//#define rson ((i<<1)|1),mid+1,r
#define uint unsigned int
typedef pair<int,int> PII;
#define A first
#define B second
#define pb push_back
#define MK make_pair
#define ll long long
template<typename T>
void read1(T &m) {
T x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9') {
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=x*10+ch-'0';
ch=getchar();
}
m = x*f;
}
template<typename T>
void read2(T &a,T &b) {
read1(a);
read1(b);
}
template<typename T>
void read3(T &a,T &b,T &c) {
read1(a);
read1(b);
read1(c);
}
template<typename T>
void out(T a) {
if(a>9) out(a/10);
putchar(a%10+'0');
}
template<typename T>
void outn(T a) {
if(a>9) out(a/10);
putchar(a%10+'0');
puts("");
}
using namespace std;
///------------------------------------------------------------------------------------
int SG[55][55];
int getsg(int i,int j)
{
bool flag[4000];
memset(flag,false,sizeof(flag));
rep0(k,0,i)
rep0(l,0,j)
flag[SG[k][j]^SG[i][l]] = true;
rep0(k,0,4000) if(!flag[k]) return k;
}
void inti(void)
{
MS_1(SG);
rep0(i,0,55)
SG[i][0] = SG[0][i] = i;
rep0(i,1,55)
rep0(j,1,55)
SG[i][j]=getsg(i,j);
}
char x[55];
int main() {
inti();
int a,b;
while(read2(a,b),a||b)
{
int ans = 0;
rep0(i,0,a)
{
scanf("%s",x);
rep0(j,0,b)
if(x[j]=='#')
ans^=SG[i][j];
}
puts(ans?"John":"Jack");
}
return 0;
}