標籤:
題目連結:
http://acm.hdu.edu.cn/showproblem.php?pid=5546
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 cell board, the chess can be put on the intersection of the board lines, so there are 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, . 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. represents a cell with black chess which owned by Yu Zhou. represents a cell with white chess which owned by Su Lu.
Output
For each test case, output one line containing
Case #x: y, where is the test case number (starting from 1) and 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.
真是要瘋了,一道題並不難的題,由於題意一直讀不懂,愣是弄了快3個小時才OK
Mean:
我方棋子是‘x‘,敵方是‘o‘。現在輪到我方落子,問我方能不能在下一回合吃掉對方的至少一個棋子。
吃掉的規則是:對方被圍的棋子在下一回合時已經找不到為‘ . ‘的位置,就是我下一個棋子把‘o’包圍了。
範圍:棋盤大小9*9。
Analse:
枚舉每一個‘.‘,使其變成‘x‘,在這一點周圍四個點為方向進行DFS,遇到‘x‘返回,如果存在一個DFS過程中沒發現‘.‘即存在。
#include <cstdio>#include <cstring>#include <iostream>#include <cmath>#include<vector>#include<queue>#include<algorithm>using namespace std;typedef long long LL;const int maxn=1000005;const int INF=0x3f3f3f3f;char maps[10][10];int vis[10][10];int dir[4][2]= {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};int f;void DFS(int x, int y){ vis[x][y]=1; if(maps[x][y]==‘.‘) { f=1; return ; } if(maps[x][y]==‘x‘)return ; for(int i=0; i<4; i++) { int nx=x+dir[i][0]; int ny=y+dir[i][1]; if(nx>=0&&nx<9&&ny>=0&&ny<9&&!vis[nx][ny]) DFS(nx, ny); }}int solve(){ for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { if(maps[i][j]==‘.‘)///枚舉每一個‘.‘ { maps[i][j]=‘x‘;///將其變成黑棋 for(int k=0; k<4; k++) { int nx=i+dir[k][0]; int ny=j+dir[k][1]; if(nx>=0&&nx<9&&ny>=0&&ny<9&&maps[nx][ny]==‘o‘)///尋找‘.‘周圍合法的白棋 { memset(vis, 0, sizeof(vis)); f=0; DFS(nx, ny);///判斷白棋是否能夠被我方黑棋包圍 if(!f) return 1; } } maps[i][j]=‘.‘;///如果其周圍沒有白棋或者黑棋包圍白棋不成功,還要將其還原為‘.‘ } } } return 0;}int main(){ int T, cas=1; scanf("%d", &T); while(T--) { for(int i=0; i<9; i++) scanf("%s", maps[i]); int ans=solve(); if(ans)printf("Case #%d: Can kill in one move!!!\n", cas++); else printf("Case #%d: Can not kill in one move!!!\n", cas++); } return 0;}
hdu---5546---Ancient Go