hdu 4678 Mine(博弈SG,4级)

Mine

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 487    Accepted Submission(s): 134


Problem Description
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.
 

Input
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
 

Output
For each case, first you should print "Case #x: ", where x indicates the case number between 1 and T . Then output the winner of the game, either ”Xiemao” or “Fanglaoshi”. (without quotes)
 

Sample Input
2 3 3 0 3 3 1 1 1
 

Sample Output
Case #1: Xiemao Case #2: Fanglaoshi
 

Source
 

Recommend
zhuyuanchen520

思路:单独一个空白块是必胜点sg=1

            一个空白块和多个数字点,一个数字点和一个空白快,取空白块得->sg=0 取数字转移到1故sg=2

            一空2 数字,取空->sg=0 取数字 sg=2 故sg=1

                类推,sg=数字点%2+1

       纯数字点,为一单独块, sg=1


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int dx[]={0,0,1,-1,1,1,-1,-1};
const int dy[]={1,-1,0,0,1,-1,1,-1};
const int mm=1050;
bool g[mm][mm],vis[mm][mm];
int n,m;
class Edge
{
  public:int x,y;
};
bool check(int x,int y)
{
  if(x>0&&g[x-1][y])return 1;
  if(x<n-1&&g[x+1][y])return 1;
  if(y>0&&g[x][y-1])return 1;
  if(y<m-1&&g[x][y+1])return 1;
  if(x>0&&y>0&&g[x-1][y-1])return 1;
  if(x>0&&y<m-1&&g[x-1][y+1])return 1;
  if(x<n-1&&y>0&&g[x+1][y-1])return 1;
  if(x<n-1&&y<m-1&&g[x+1][y+1])return 1;
  return 0;
}
int bfs(int x,int y)
{ int num=0;
  queue<Edge>Q;
  Edge tmp,t;vis[x][y]=1;
  tmp.x=x;tmp.y=y;Q.push(tmp);
  while(!Q.empty())
  {
    tmp=Q.front();Q.pop();
    FOR(i,0,7)
    {
      t.x=tmp.x+dx[i];
      t.y=tmp.y+dy[i];
      if(t.x<0||t.y<0||t.x>=n||t.y>=m)continue;
      if(g[t.x][t.y]||vis[t.x][t.y])continue;
      vis[t.x][t.y]=1;
      if(!check(t.x,t.y))
      {
        Q.push(t);continue;
      }
      ++num;
    }
  }
  return num;
}
int main()
{
  int cas,K,a,b;
 // freopen("data.in","r",stdin);
  while(~scanf("%d",&cas))
  {
    FOR(ca,1,cas)
    { clr(g,0);
      scanf("%d%d%d",&n,&m,&K);
      FOR(i,1,K)
      {
        scanf("%d%d",&a,&b);
        g[a][b]=1;
      }
      clr(vis,0);
      int ans=0;
      FOR(i,0,n-1)FOR(j,0,m-1)
      if(!g[i][j]&&!vis[i][j]&&!check(i,j))
        {
          int tmp=bfs(i,j);
          ans^=(tmp%2+1);
        }
      FOR(i,0,n-1)FOR(j,0,m-1)
      if(!g[i][j]&&!vis[i][j]&&check(i,j))
        ans^=1;
        printf("Case #%d: ",ca);
      if(ans)printf("Xiemao\n");
      else printf("Fanglaoshi\n");
    }
  }
}



posted @ 2013-08-16 14:01  剑不飞  阅读(161)  评论(0编辑  收藏  举报