hdu 5546(DFS或BFS)

来源:互联网 发布:皮革护理 知乎 编辑:程序博客网 时间:2024/06/08 13:28

问题描述:

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!!!

题目题意:题目给了我们一个棋盘,问x棋子能否下一步的情况下,逼死一个或者一块o棋子。

题目分析:dfs和bfs都可以做,遍历所有的o棋子,看它的周围是否存在超过一个的'.'棋子。有俩个地方需要注意。

1:我们的标记数组除了标记‘o'之外,还需要标记'.',避免重复计算'.'棋子

2:每次搜索后,需要清空vis标记数组。测试一下样例,如果第二组错了,输出找到的i,j坐标,就会明白了。

代码如下:

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;char mmap[10][10];bool vis[10][10];int Next[4][2]={{-1,0},{1,0},{0,1},{0,-1}};int num,flag;void dfs(int x,int y){    if (num>=2) return ;    for (int i=0;i<4;i++) {        int dx=x+Next[i][0];        int dy=y+Next[i][1];         if (dx<0||dx>=9||dy<0||dy>=9||vis[dx][dy]||mmap[dx][dy]=='x') continue;         if(mmap[dx][dy]=='o') {            vis[dx][dy]=true;            dfs(dx,dy);         }         else if(mmap[dx][dy]=='.'){            vis[dx][dy]=true;            num++;         }    }}int main(){    int t,icase=1;    scanf("%d",&t);    while (t--) {        for (int i=0;i<9;i++)            scanf("%s",mmap[i]);        memset (vis,false,sizeof (vis));        num=flag=0;        for (int i=0;i<9;i++) {            for (int j=0;j<9;j++) {                if (mmap[i][j]=='o'&&!vis[i][j]) {                    memset (vis,false,sizeof (vis));                    num=0;                    dfs(i,j);                    if (num==1) {                        flag=1;                        break;                    }                }            }            if (flag) break;        }        if (flag)            printf("Case #%d: Can kill in one move!!!\n",icase++);        else            printf("Case #%d: Can not kill in one move!!!\n",icase++);    }    return 0;}
#include<iostream>#include<cstring>#include<cstdio>#include<queue>#include<algorithm>using namespace std;char str[15][15];struct node{    int x;    int y;}p,tmp;queue<node> que;bool vis[15][15];int _next[4][2]={1,0,-1,0,0,1,0,-1};bool check(int x,int y){    if(x<1||x>9||y<1||y>9||str[x][y]=='x'||vis[x][y])        return false;    return true;}int bfs(){    int num=0;    while(que.size())    {        tmp=que.front();        que.pop();        for(int i=0;i<4;i++)        {            int tx=tmp.x+_next[i][0];            int ty=tmp.y+_next[i][1];            if(check(tx,ty))            {                vis[tx][ty]=true;                if(str[tx][ty]=='.')                    num++;                else                {                    p.x=tx;                    p.y=ty;                    que.push(p);                }            }        }    }    return num;}int main(){    int t;    scanf("%d",&t);    int cas=1;    while(t--)    {        for(int i=1;i<=9;i++)            scanf("%s",str[i]+1);        memset(vis,false,sizeof(vis));        int flag=0;        for(int i=1;i<=9;i++){            for(int j=1;j<=9;j++){                if(str[i][j]=='o'&&!vis[i][j]){                    p.x=i;                    p.y=j;                    que.push(p);                    vis[i][j]=true;                    memset(vis,false,sizeof(vis));                    if(bfs()==1){                        //cout<<bfs()<<' '<<i<<' '<<j<<endl;                        flag=1;                        break;                    }                }            }            if(flag) break;        }        if(flag) printf("Case #%d: Can kill in one move!!!\n",cas++);        else printf("Case #%d: Can not kill in one move!!!\n",cas++);    }    return 0;}