借ds大牛的一句话,此题是一个道很基础的sg题
不知道的可以看这里
http://blog.csdn.net/ayecsz/article/details/10078749
http://blog.csdn.net/ayecsz/article/details/10079047
每个格子可以是数字或者空白格,点空白格可以将这一块的空白格和周围的数字格都显示出来,所以我们可以用BFS将方格进行分块。这样就抽象为了Nim博弈问题,有多少块就有多少堆石子,每堆石子每次可取1个或全部取完,所以sg[0] = 0,sg[1] = 1; sg[2] = 2; sg[3] = 1; sg[4] = 2; sg[5] = 1........ (1,2循环)然后做一次异或操作即可。
注意:每堆石子个数 = 数字格个数+是否有空白格(有是1,没有是0),因为将数字格点完后还可以点一次空白格。
其实感觉这道题主要还是考BFS。。。
c++代码:
#include<cstdio>
#include<cstring>
const int c1[8] = {-1,-1,-1,0,1,1,1,0};
const int c2[8] = {-1,0,1,1,1,0,-1,-1};
const int MAX = 1002;
int n,m,k;
int a[MAX][MAX],num[MAX*MAX];
bool flag[MAX][MAX];
struct
{
int x,y;
}que[MAX*MAX];
int bfs(int fx,int fy)
{
int x,y,head,tail,total,l;
que[1].x = fx;
que[1].y = fy;
flag[fx][fy] = true;
head = 0; tail = 1;
total = 1;
while (head < tail)
{
head++;
x = que[head].x;
y = que[head].y;
if (a[x][y] == 0)
{
for (l = 0; l < 8; l++)
if (flag[x+c1[l]][y+c2[l]] == false)
{
tail++;
que[tail].x = x+c1[l];
que[tail].y = y+c2[l];
flag[x+c1[l]][y+c2[l]] = true;
}
}
else total++;
}
return total;
}
int main()
{
int temp,kase,xx,yy,x,y,tot,ans,i,j,l;
scanf("%d",&temp);
for (kase = 1; kase <= temp; kase++)
{
scanf("%d%d%d",&n,&m,&k);
memset(a,0,sizeof(a));
memset(flag,false,sizeof(flag));
for (i = 0; i <= n+1; i++) flag[i][m+1] = flag[i][0] = true;
for (i = 0; i <= m+1; i++) flag[n+1][i] = flag[0][i] = true;
//
for (i = 1; i <= k; i++)
{
scanf("%d%d",&x,&y);
x++; y++;
a[x][y] = -1;
flag[x][y] = true;
for (l = 0; l < 8; l++)
{
xx = x + c1[l];
yy = y + c2[l];
if (a[xx][yy] != -1) a[xx][yy]++;
}
}
tot = 0;
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
if (a[i][j] == 0 && flag[i][j] == false)
{
num[++tot] = bfs(i,j);
if (num[tot] % 2 == 0) num[tot] = 2;
else num[tot] = 1;
}
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
if (flag[i][j] == false)
{
num[++tot] = 1;
}
ans = 0;
for (i = 1; i <= tot; i++)
ans = ans^num[i];
printf("Case #%d: ",kase);
if (ans == 0) printf("Fanglaoshi\n");
else printf("Xiemao\n");
}
return 0;
}