HDU - 5546
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5546
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.
题意:
一个9*9 的棋盘,然后x代表黑棋,o代表白旗,". “代表的是空白位置;
让你用黑棋走一步就是放在(”." 空白的位置)围住白棋,当白棋周围都是黑棋时,就可以吃掉这个白棋,那么就输出Can kill in one move!!!,否则输出 Can not kill in one move!!!;
这个可以理解为围棋。
解题思路:
用搜索,这个用dfs;要搜索白棋周围是否存在一个空白的位置放一个黑棋,然后使得这个黑棋刚好可以围住白棋,那么就成功,否则就失败;
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;
int vis[15][15], cnt, m, n, fx, fy, run[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
char s[15][15];
void dfs(int x, int y)
{
if(s[x][y] == '.'){
cnt ++;
return ;
}
for(int i = 0; i<4; i++)
{
int a = x + run[i][0];
int b = y + run[i][1];
if(s[a][b] != 'x' && !vis[a][b] && a>=1 && a<=9 && b>=1 && b<=9)
{
vis[a][b] = 1;
dfs(a, b);
}
}
}
int main()
{
int t;
cin>>t;
for(int cas = 1; cas<=t; cas++)
{
for(int i = 0; i<=10; i++)//围住就不会有越界的影响
{
s[0][i] = 'x';
s[i][0] = 'x';
s[10][i] = 'x';
s[i][10] = 'x';
}
for(int i = 1; i<=9; i++)//输入棋盘
for(int j = 1; j<=9; j++)
scanf(" %c", &s[i][j]);
// for(int i = 0; i<=10;i++)
// {
// for(int j = 0; j<=10; j++)
// printf("%c", s[i][j]);
// printf("\n");
// }
int ok = 0;
for(int i = 1; i<=9; i++)//开始搜索
{
for(int j = 1; j<=9; j++)
{
if(s[i][j] == 'o')//是否是白棋
{
memset(vis, 0, sizeof(vis));//这里每次进行dfs时vis标记都清零
cnt = 0;
dfs(i, j);//每个位置都进行搜索
if(cnt == 1)//存在一个空白的位置使得黑岂能包围白棋;
{
ok = 1;
break;
}
}
}
if(ok)//搜索成功
break;
}
if(ok) printf("Case #%d: Can kill in one move!!!\n", cas);
else printf("Case #%d: Can not kill in one move!!!\n", cas);
}
return 0;
}