HDU 5546 Ancient Go (dfs)

来源:互联网 发布:mac中我的所有文件 编辑:程序博客网 时间:2024/06/05 09:08

Ancient Go

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1670    Accepted Submission(s): 533


Problem Description
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 are9×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(1T100).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.
 

Source
The 2015 China Collegiate Programming Contest 


题意:一个9x9的棋盘,o代表白子,x代表黑子,.代表还没有棋子下在该位置。现在要判断是否再下一个黑子可以使白子被围住。

思路:一开始题意很明确,也知道用dfs,但并没有想好怎么用。后来看了题解还是有点模棱两可。之后想想明白了,对于每个没有下过棋子的位置都把它变成x判断一下是否能搜到.,如果能搜到代表不能被围住。如果搜不到代表可以被围住。


#include <iostream>#include <string.h>#include <algorithm>#include <math.h>using namespace std;char c[15][15];int visit[15][15];int flag;int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};void dfs(int x,int y){if(c[x][y]=='.'){flag=1;return;}if(c[x][y]=='x')return;for(int i=0;i<4;i++){int dx=x+dir[i][0];int dy=y+dir[i][1];if(dx<0||dy<0||dx>=9||dy>=9||visit[dx][dy]==1)continue;visit[dx][dy]=1;dfs(dx,dy);}}int solve(){for(int i=0;i<9;i++){for(int j=0;j<9;j++){if(c[i][j]=='.'){c[i][j]='x';for(int k=0;k<4;k++){int dx=i+dir[k][0];int dy=j+dir[k][1];if(dx<0||dx>=9||dy<0||dy>=9)continue;if(c[dx][dy]=='o'){memset(visit,0,sizeof(visit));visit[dx][dy]=1;flag=0;dfs(dx,dy);if(!flag)return 1;}}c[i][j]='.';}}}return 0;}int main(){int t;scanf("%d",&t);for(int cas=1;cas<=t;cas++){for(int i=0;i<9;i++){scanf("%s",c[i]);}memset(visit,0,sizeof(visit));int ans=solve();printf("Case #%d: ",cas);if(ans){printf("Can kill in one move!!!\n");}elseprintf("Can not kill in one move!!!\n");}return 0;} 


0 0
原创粉丝点击