当前位置: 代码迷 >> 综合 >> hdu 5546 Ancient Go(bfs+模拟)
  详细解决方案

hdu 5546 Ancient Go(bfs+模拟)

热度:72   发布时间:2023-11-17 14:20:16.0

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(1T100)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.


题解:

题意:

就是围棋的规则,下x的那个人问下一步是否可以至少杀对面圆的人的一个子

思路:

直接搜索每一个o,bfs搜索相连的o并标记,如果周围点的数目为1就可以赢

代码:

#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<deque>
#include<algorithm>
#define ll long long
#define INF 100861111
#define M (t[k].l+t[k].r)/2
#define lson k*2
#define rson k*2+1
using namespace std;
char s[15][15];
int vis[15][15];
int dirx[4]={0,0,-1,1};
int diry[4]={1,-1,0,0};
struct node
{int x,y;
};
queue<node>q;
int Search(int x,int y)
{int xx,yy,i;int p[15][15];memset(p,0,sizeof(p));int flag=0;while(!q.empty())q.pop();node now,next;now.x=x;now.y=y;q.push(now);vis[x][y]=1;p[x][y]=1;while(!q.empty()){now=q.front();q.pop();for(i=0;i<4;i++){xx=now.x+dirx[i];yy=now.y+diry[i];if(xx<0||yy<0||xx>=9||yy>=9||p[xx][yy])continue;if(s[xx][yy]=='.'){p[xx][yy]=1;flag++;}if(s[xx][yy]=='o'){p[xx][yy]=1;vis[xx][yy]=1;next.x=xx;next.y=yy;q.push(next);}}}if(flag==1)return 1;return 0;
}
int main()
{int i,j,test,q,tag;scanf("%d",&test);for(q=1;q<=test;q++){for(i=0;i<9;i++){scanf("%s",s[i]);}memset(vis,0,sizeof(vis));tag=0;for(i=0;i<9;i++){for(j=0;j<9;j++){if(s[i][j]=='o'&&!vis[i][j]){if(Search(i,j)){tag=1;goto loop;}}}}loop:;printf("Case #%d: ",q);if(tag){printf("Can kill in one move!!!\n");}elseprintf("Can not kill in one move!!!\n");}return 0;
}