hdu4678 Mine 2013 Multi-University Training Contest 8 博弈题

Mine

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

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,Yimeans 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

博弈题;用SG值做:

连通的空白块和相连的数字块是一起的,一个单独的数字块是一类。

单独一个的数组块,SG是1.

空白块+若干个数字块,数字块个数为n的话,SG是n%2 + 1

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <string.h>
 5 #include <math.h>
 6 #include <vector>
 7 #include <stack>
 8 #include <queue>
 9 using namespace std;
10 #define ll long long int
11 int a[1100][1100];
12 int b[1100][1100];
13 int n,m;
14 int z[8][2];
15 void fun()
16 {
17     int i,j;
18     for(i=1;i<=n;i++)
19     {
20         for(j=1;j<=m;j++)
21         {
22             if(a[i][j]==-1)
23             {
24                 for(int r=0;r<8;r++)
25                 {
26                     if(a[i+z[r][0]][j+z[r][1]]!=-1)
27                     a[i+z[r][0]][j+z[r][1]]++;
28                 }
29             }
30         }
31     }
32 }
33 int dfs(int x,int y)
34 {
35     int i;
36     ll sum=0;
37     queue<pair<int,int> >aa;
38     while(!aa.empty())aa.pop();
39     aa.push(make_pair(x,y));
40     while(!aa.empty())
41     {
42         int fx=aa.front().first;
43         int fy=aa.front().second;
44         aa.pop();
45         for(i=0;i<8;i++)
46         {
47             if(fx+z[i][0]<=n&&fx+z[i][0]>=1)
48             if(fy+z[i][1]<=m&&fy+z[i][1]>=1)
49             if(!b[fx+z[i][0]][fy+z[i][1]])
50             {
51                 if(a[fx+z[i][0]][fy+z[i][1]]>0)sum++;
52                 else
53                 aa.push(make_pair(fx+z[i][0],fy+z[i][1]));
54                 b[fx+z[i][0]][fy+z[i][1]]=1;
55             }
56         }
57     }
58     return sum%2+1;
59 }
60 int main()
61 {
62     int t;
63     scanf("%d",&t);
64     int i,j,r;
65     z[0][0]=0;z[0][1]=1;z[4][0]=1;z[4][1]=1;
66     z[1][0]=0;z[1][1]=-1;z[5][0]=-1;z[5][1]=1;
67     z[2][0]=1;z[2][1]=0;z[6][0]=1;z[6][1]=-1;
68     z[3][0]=-1;z[3][1]=0;z[7][0]=-1;z[7][1]=-1;
69     for(r=0;r<t;r++)
70     {
71         int k,x,y;
72         scanf("%d%d%d",&n,&m,&k);
73         memset(a,0,sizeof(a));
74         memset(b,0,sizeof(b));
75         for(j=0;j<k;j++)
76         {
77             scanf("%d%d",&x,&y);
78             a[x+1][y+1]=-1;
79             b[x+1][y+1]=1;
80         }
81         fun();
82         int sum=0;
83         for(i=1;i<=n;i++)
84         for(j=1;j<=m;j++)
85         {
86             if(a[i][j]==0&&b[i][j]==0)
87             b[i][j]=1,sum^=dfs(i,j);
88         }
89         for(i=1;i<=n;i++)
90         for(j=1;j<=m;j++)
91         {
92             if(!b[i][j])
93             sum^=1;
94         }
95         if(!sum)
96         printf("Case #%d: Fanglaoshi
",r+1);
97         else printf("Case #%d: Xiemao
",r+1);
98     }
99 }
View Code
原文地址:https://www.cnblogs.com/ERKE/p/3261168.html