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×88×8 cell board, the chess can be put on the intersection of the board lines, so there are 9×99×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(1≤T≤100). TT 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′′x′ represents a cell with black chess which owned by Yu Zhou. ′o′′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 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.
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.
Here is the rules for ancient go they were playing:
⋅⋅The game is played on a 8×88×8 cell board, the chess can be put on the intersection of the board lines, so there are 9×99×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(1≤T≤100). TT 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′′x′ represents a cell with black chess which owned by Yu Zhou. ′o′′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 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.
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.
看到题目就可以发现这是一个dfs,不过需要一些操作, 题意就是把一个 ‘.’改成'x', 让'o'的周围被‘x’所围成,注意被'x'所围成的图案里面没有‘.‘,只有‘o’,数据比较少,只要简单的暴力dfs和回溯下就可以过。以后要每天一题去完成我的ACM之路,不过教主今天上着课暴走了,以后也要为队里做出点贡献,尽量让每个题解都写更详细,不能像前几个直接把AC的代码贴出来了,希望这个博客可以见证我的成长。
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iostream>
#include <string>
using namespace std;
char s[20][20], ss[20][20];
bool visited[20][20];
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
bool flag = 0;
bool in(int x, int y)
{
return x >= 0 && y >= 0 && x < 9 && y < 9;
}
bool dfs(int x, int y)
{
if(!in(x, y)|| visited[x][y]||s[x][y] == 'x') // 如果到边界,遍历过或者为'x'则证明符合条件
return 1;
if(s[x][y] == '.') //如果有’.‘在这'o'的旁边说明这个地方不符合条件,返回false
return 0;
visited[x][y] = 1;// 防止多次遍历
for(int i = 0; i < 4; i++)
{
if(!dfs(x + dx[i], y + dy[i])) //如果x,y 对应的为'o'则需要继续遍历下面的
return 0;
}
return 1; //全部遍历都符合条件后就对
}
void check()
{
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
if(s[i][j] == 'o') // 从'o'开始遍历,看这个点时候符合条件
{
memset(visited, 0, sizeof(visited)); // 初始化
if(dfs(i, j))
flag = 1;
}
}
}
}
int main()
{
int T, kase = 0;
cin >> T;
while(T--) // 样例个数
{
flag = 0;
for(int i = 0; i < 9; i++)
cin >> s[i];
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
if(s[i][j] == '.') // 判断是否为 ‘.’ 如果是就进行判断
{
s[i][j] = 'x';
check();
s[i][j] = '.'; //回溯为原来的情况
}
}
}
if(flag)
printf("Case #%d: Can kill in one move!!!\n", ++kase);
else
printf("Case #%d: Can not kill in one move!!!\n", ++kase);
}
return 0;
}