hdu5546 Ancient Go(bfs)

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×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(1≤T≤100)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


题意

在一个9*9的棋盘上,两人对弈。x表示黑子,o表示白子,其余位置用 . 表示。相连的同色棋子构成一个连通块。若连通块内的每个棋子四周都无空位置,则该块被对方吃掉。现问能不能在某个地方落一个黑子,使得至少一个白子立刻被吃掉。

思路

对每个连通块bfs,搜索并记录周围空位(即“.”)的个数。如果空位个数==1,则该块可以立即被吃掉,返回true;否则返回false。

只要有一个连通块返回true,题目就答案为yes;否则为no。

一开始以为只统计o外部的空位,后来发现无论外部内部只要还有空位就算活着,都要统计。这样就好写多了。
Still,有一处标记 vis[x][y]=true,=写成了==,找了好久。

代码

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
char G[12][12];
int dr[]={1, 0, -1, 0}, dc[]={0, 1, 0, -1};
int num;
bool vis[12][12];
queue<pair<int, int> > q;
bool ok(int x, int y){
    if(x>=0&&x<9&&y>=0&&y<9) return true;
    return false;
}
int bfs(int sr, int sc){
    memset(vis, 0, sizeof(vis));
    num=0;
    vis[sr][sc]=true;
    q.push(make_pair(sr, sc));
    while(!q.empty()){
        int r=q.front().first, c=q.front().second;
        q.pop();
        for(int i=0; i<4; i++){
            int x=r+dr[i], y=c+dc[i];
            if(!ok(x, y)) continue;
            if(!vis[x][y]&&G[x][y]=='.'){
                vis[x][y]=true;
                num++;
            }
            else if(!vis[x][y]&&G[x][y]=='o'){
                vis[x][y]=true;
                q.push(make_pair(x, y));
            }
        }   
    }
    return num;
}
int main(void){
    int T; scanf("%d", &T);
    int Cas=0;
    getchar();
    while(T--){
        memset(vis, 0, sizeof(vis));
        getchar();
        for(int i=0; i<9; i++){
            scanf("%s", G[i]);
        }
        bool flag=false;
        for(int i=0; i<9; i++){
            for(int j=0; j<9; j++){
                if(G[i][j]=='o'&&!vis[i][j]){
                    if(bfs(i, j)==1){
                        flag=true;
                        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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值