2015
09-17

# Mine

Have you ever played a game in Windows: Mine?
This game is played on a n*m board, just like the Pic(1)

On the board, Under some grids there are mines (represent by a red flag). There are numbers ‘A(i,j)’ on some grids means there’re A(i,j) mines on the 8 grids which shares a corner or a line with gird(i,j). Some grids are blank means there’re no mines on the 8 grids which shares a corner or a line with them.
At the beginning, all grids are back upward.
In each turn, Player should choose a back upward grid to click.
If he clicks a mine, Game over.
If he clicks a grid with a number on it , the grid turns over.
If he clicks a blank grid, the grid turns over, then check grids in its 8 directions.If the new grid is a blank gird or a grid with a number,it will be clicked too.
So If we click the grid with a red point in Pic(1), grids in the area be encompassed with green line will turn over.
Now Xiemao and Fanglaoshi invent a new mode of playing Mine. They have found out coordinates of all grids with mine in a game. They also find that in a game there is no grid will turn over twice when click 2 different connected components.(In the Pic(2), grid at (1,1) will turn over twice when player clicks (0,0) and (2,2) , test data will not contain these cases).
Then, starting from Xiemao, they click the grid in turns. They both use the best strategy. Both of them will not click any grids with mine, and the one who have no grid to click is the loser.
Now give you the size of board N, M, number of mines K, and positions of every mine Xi,Yi. Please output who will win.

Multicase
The first line of the date is an integer T, which is the number of the text cases. (T<=50)
Then T cases follow, each case starts with 3 integers N, M, K indicates the size of the board and the number of mines.Then goes K lines, the ith line with 2 integer Xi,Yi means the position of the ith mine.
1<=N,M<=1000 0<=K<=N*M 0<=Xi<N 0<=Yi<M

Multicase
The first line of the date is an integer T, which is the number of the text cases. (T<=50)
Then T cases follow, each case starts with 3 integers N, M, K indicates the size of the board and the number of mines.Then goes K lines, the ith line with 2 integer Xi,Yi means the position of the ith mine.
1<=N,M<=1000 0<=K<=N*M 0<=Xi<N 0<=Yi<M

2
3 3 0
3 3 1
1 1

Case #1: Xiemao
Case #2: Fanglaoshi

题意就不说了，太长了。。。

这个应该算简单博弈吧。先求联通分量，把空白区域边上的数字个数全部求出来a[i](就是一个连通分量)，然后就是n堆石子，每堆每次可以取一个或者全部取掉，然后要注意在取玩边上的石子后，剩下的就只能一次取掉了，因此我们直接把空白区域上的算做一个a[i]+1。然后这个SG函数很好求，奇数是1，偶数是2。。。

 //STATUS:C++_AC_156MS_4268KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=1010;
const int INF=0x3f3f3f3f;
const LL MOD=1000000007,STA=8000010;
const LL LNF=1LL<<55;
const double EPS=1e-9;
const double OO=1e50;
const int dx[8]={-1,-1,0,1,1,1,0,-1};
const int dy[8]={0,1,1,1,0,-1,-1,-1};
const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End

int g[N][N];
int T,n,m,k;

int bfs(int x,int y)
{
int i,nx,ny,ret=0;
pii t;
g[x][y]=-1;
queue<pii> q;
q.push(make_pair(x,y));
while(!q.empty())
{
t=q.front();q.pop();
for(i=0;i<8;i++){
nx=t.first+dx[i];
ny=t.second+dy[i];
if(nx<0||nx>=n || ny<0||ny>=m || g[nx][ny]==-1)continue;
if(g[nx][ny])ret++;
else q.push(make_pair(nx,ny));
g[nx][ny]=-1;
}
}
return ret;
}

int main(){
//   freopen("in.txt","r",stdin);
int i,j,sg,x,y,nx,ny,ca=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
mem(g,0);
for(i=0;i<k;i++){
scanf("%d%d",&x,&y);
g[x][y]=-1;
for(j=0;j<8;j++){
nx=x+dx[j];ny=y+dy[j];
if(nx<0||nx>=n || ny<0||ny>=m || g[nx][ny]==-1)continue;
g[nx][ny]=1;
}
}

sg=0;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(g[i][j])continue;
sg^=(bfs(i,j)&1)+1;
}
}
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(g[i][j]==-1)continue;
sg^=1;
}
}

printf("Case #%d: %s\n",ca++,sg?"Xiemao":"Fanglaoshi");
}
return 0;
}