题目链接:https://vjudge.net/problem/HDU-5546
Ancient Go
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.
Input
The first line of the input gives the number of test cases, T(1≤T≤100). T test 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.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y 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.
Sample Input
2
.......xo
.........
.........
..x......
.xox....x
.o.o...xo
..o......
.....xxxo
....xooo.
......ox.
.......o.
...o.....
..o.o....
...o.....
.........
.......o.
...x.....
........o
Sample Output
Case #1: Can kill in one move!!!
Case #2: Can not kill in one move!!!
Hint
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.
题意:周瑜和鲁肃在下棋,周瑜下的是黑棋用x表示,鲁肃下的是白棋用o表示,
规则大概是如果这个棋子的周围被另一种棋子围住且与其他空格不相连,则这个棋子或者这些棋子就被吃了,问题是现在轮到周瑜下棋了,让我们判断他下一步棋能不能吃掉对手至少一枚棋子。
思路:可以直接找"o",来判断它周围(上下左右)有多少个"."(即可以下的地方),如果只有一个地方可以下且其他地方都被"x"包围了则这枚棋子就可以被吃掉,如果有"o"连续的也是一样,通过搜索来确定他们周围一共有多少".",如果这个部分周围只有一个地方可以走的话,这个部分也可以被吃掉。
可以用dfs实现。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<iomanip>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
char mp[20][20];
int book[20][20],book1[20][20];
int mov[4][2]= {0,-1,-1,0,1,0,0,1};
int cnt;
void dfs(int x,int y)
{
if(mp[x][y]=='o')
book[x][y]=1;
for(int i=0; i<4; i++)
{
int ex=x+mov[i][0];
int ey=y+mov[i][1];
if(ex>=0&&ey>=0&&ex<9&&ey<9&&mp[ex][ey]=='.'&&!book1[ex][ey]) //’o‘周围的‘.’的个数
{
cnt++;
book1[ex][ey]=1;
}
else if(ex>=0&&ey>=0&&ex<9&&ey<9&&mp[ex][ey]=='o'&&!book[ex][ey]) //如果‘o’的旁边有‘o’,再进行一次搜索
dfs(ex,ey);
}
}
int main()
{
int T,t=1;
scanf("%d",&T);
while(T--)
{
memset(book,0,sizeof(book));
for(int i=0; i<9; i++)
scanf("%s",mp[i]);
int flag=0;
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
{
if(mp[i][j]=='o'&&!book[i][j])
{
memset(book1,0,sizeof(book1));
cnt=0;
dfs(i,j);
if(cnt==1)
{
flag=1;
break;
}
}
}
if(flag)
break;
}
printf("Case #%d: ",t++);
if(flag==0)
printf("Can not kill in one move!!!\n");
else
printf("Can kill in one move!!!\n");
}
return 0;
}