HDU 5546 - Ancient Go(DFS)

Ancient Go

TimeLimit: 3000/1000 MS (Java/Others)    Memory Limit:65535/65535 K (Java/Others)
Total Submission(s): 2952    Accepted Submission(s): 934

ProblemDescription

Yu Zhou likes to play Go with SuLu. From the historical research, we found that there are much difference onthe 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 boardlines, so there are9×9 different positions to put the chess.
 Yu Zhou always takes the black and Su Lu the white. They put the chessonto the game board alternately.
 The chess of the same color makes connected components(connected bythe board lines), for each of the components, if it's not connected with any ofthe 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 componentsfirst. After removing the dead opponent's components, check with the player's componentsand remove the dead components.
One day, Yu Zhou was playing ancient go with Su Lu at home. It's Yu Zhou's movenow. But they had to go for an emergency military action. Little Qiao looked atthe game board and would like to know whether Yu Zhou has a move to kill atleast one of Su Lu's chess.

 

 

Input

The first line of the input gives thenumber of test cases, T(1T100)T  test cases follow. Test cases are separated by an empty line. Eachtest case consist of 9 lines represent the game board. Each line consists of 9characters. 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 linecontaining Case #x: y, wherexy  isCan kill in one move!!! if Yu Zhou has a move to kill atleast 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!!!

【题意】

 给定一个9*9围棋棋盘,我方棋子是'x',敌方是'o',空地是'.',现在轮到我方下棋,判断下一步能否吃掉对面的棋子。


【思路】

 对每一个'o'进行dfs搜索,如果这个o联通区域的周围只有一个'.',其他的全是'x'那么下一步就可以吃掉,否则就不行。遍历所有的'o'联通块,每次遍历时用cnt记录周围'.'的个数,这里用了一个vis数组避免重复记录。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 9;
const int dx[4] = { 0, 1, 0, -1 };
const int dy[4] = { 1, 0, -1, 0 };

int cnt;
char g[maxn][maxn];
bool used[maxn][maxn];
bool vis[maxn][maxn];

void dfs(int r, int c) {
	used[r][c] = 1;
	for (int k = 0; k < 4; ++k) {
		int x = dx[k] + r;
		int y = dy[k] + c;
		
		if (x < 0 || x >= maxn || y < 0 || y >= maxn) continue;
		if (!vis[x][y] && g[x][y] == '.') {
			vis[x][y] = 1;
			++cnt;
		}
		if (used[x][y]) continue;
		if ('o' != g[x][y]) continue;
		
		dfs(x, y);
	}
}

int main() {
	int t;
	scanf("%d", &t);
	for (int kase = 1; kase <= t; ++kase) {
		for (int i = 0; i < 9; ++i) scanf("%s", g[i]);
		memset(used, 0, sizeof(used));
		int flag = 0;
		
		for (int i = 0; i < maxn; ++i) {
			for (int j = 0; j < maxn; ++j) {
				if (!used[i][j] && 'o' == g[i][j]) {
					memset(vis, 0, sizeof(vis));
					cnt = 0;
					dfs(i, j);
					if (cnt == 1) {
						flag = 1;
						goto End;
					}
				}
			}
		}
		
		End:
			printf("Case #%d: %s\n", kase, flag ? "Can kill in one move!!!" : "Can not kill in one move!!!");
	}
	return 0;
}


posted @ 2018-01-06 22:43  不想吃WA的咸鱼  阅读(191)  评论(0编辑  收藏  举报