题目:Yu Zhou likes to play Go with Su Lu. From the historical research, we found that there are much difference on the rules between ancient go and modern go.
Here is the rules for ancient go they were playing:
⋅⋅The game is played on a 8×8 cell board, the chess can be put on the intersection of the board lines, so there are 9×9 different positions to put the chess.
⋅⋅Yu Zhou always takes the black and Su Lu the white. They put the chess onto the game board alternately.
⋅⋅The chess of the same color makes connected components(connected by the board lines), for each of the components, if it's not connected with any of the empty cells, this component dies and will be removed from the game board.
⋅⋅When one of the player makes his move, check the opponent's components first. After removing the dead opponent's components, check with the player's components and remove the dead components.
One day, Yu Zhou was playing ancient go with Su Lu at home. It's Yu Zhou's move now. But they had to go for an emergency military action. Little Qiao looked at the game board and would like to know whether Yu Zhou has a move to kill at least one of Su Lu's chess.
输入:
The first line of the input gives the number of test cases, T(1≤T≤100. Ttest cases follow. Test cases are separated by an empty line. Each test case consist of 9 lines represent the game board. Each line consists of 9 characters. Each character represents a cell on the game board. ′.′ represents an empty cell. ′x′represents a cell with black chess which owned by Yu Zhou. ′o′represents a cell with white chess which owned by Su Lu.
输出:For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is Can kill in one move!!! if Yu Zhou has a move to kill at least one of Su Lu's components. Can not kill in one move!!! otherwise.
样例输入:
2 .......xo ......... ......... ..x...... .xox....x .o.o...xo ..o...... .....xxxo ....xooo. ......ox. .......o. ...o..... ..o.o.... ...o..... ......... .......o. ...x..... ........o
样例输出:
Case #1: Can kill in one move!!! Case #2: Can not kill in one move!!!
提示:
In the first test case, Yu Zhou has 4 different ways to kill Su Lu's component. In the second test case, there is no way to kill Su Lu's component.
这个题的大体意思就是查看o周围是否没有‘.’,如果正好有一个. 那么就可以杀死 su lu的一部分棋,就可以输出能够杀死。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
char a[10][10];
int vis[110][110];//这个地方千万要注意不能定义的太大, 否则就会超出空间限制
int ds[4][2]={{1,0},{-1,0},{0,1},{0,-1}};//上、下、左、右四种走法
int xx,yy,s;
struct node
{
int a,b;
}f[100];
int dfs(int x,int y)
{
int sum=0;
for(int i=0;i<4;i++)
{
int xi=x+ds[i][0];
int yi=y+ds[i][1];
if(xi>=0 &&xi<9 &&yi>=0 &&yi< 9 &&!vis[xi][yi])
{
if(a[xi][yi]=='.')
{
vis[xi][yi]=1;
sum++;
}
else if(a[xi][yi]=='o')//如果从o的地方到下一个地方还是o,那就从此刻的o再dfs(想象连通块)
{
vis[xi][yi]=1;
sum+=dfs(xi,yi);
}
}
}
return sum;
}
int main()
{
int t;
cin>>t;
int cas=0;//这个定义主要是为了输出的地方
while(t--)
{
int flag=0;
s=0;
for(int i=0;i<9;i++)
{
scanf("%s",a[i]);
for(int j=0;j<9;j++)
{
if(a[i][j]=='o')//因为o不止一个,所以定义了一个结构体,将所有的o的坐标记录下来,在下面的for循环里面挨个试
{
f[s].a=i;
f[s++].b=j;
}
}
}
printf("Case #%d: ",++cas);//这里注意题目中输出的格式,避免不必要的提交次数
for(int i=0;i<s;i++)
{
memset(vis,0,sizeof(vis));
vis[f[i].a][f[i].b]=1;
int m=dfs(f[i].a,f[i].b);
if(m==1) //这里不能写else ,因为我们是想一遇到m等于1的时候就跳出循环,例如,万一第一次m不是1,那就会输出“不会杀死”,继续下一个循环,这样就会输出很多东西
{
printf("Can kill in one move!!!\n");
flag=1;
break;
}
}
if(!flag)//在一开始定义flag为0,这样如果上面的for循环中m不等于1,那!flag就等于1,就会输出“不会杀死”,如果上面for循环里输出了“会杀死”,然后令flag为1,退出循环后!flag就是0,就不会再输出“不会杀死”
{
printf("Can not kill in one move!!!\n");
}
}
return 0;
}