hdu 5546 Ancient Go

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35323001/article/details/53248633

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5546

题目描述:

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 are 9×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 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.

题目大意:

建立在围棋的模型下,周瑜持x子,鲁肃持o子,现在给帮你一份棋盘部署,轮到周瑜下子,问是否可以通过下一个子吃掉鲁肃至少一个子,吃掉子的规则是o子旁边没有空余的.

题目分析:

9*9的方格并不是很大,所以我们可以枚举每一个o点,对于单独的o点,只需要看它周围.点的数目,如果>=2则不能被吃掉;对于两个及两个以上的o相连的区域来说,我们则需要计算出这个连通块与多少个.点相邻,如果>=2则不可以被吃掉;

AC代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
//dfs搜索上下左右四个点
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};

const int maxn=15;
int T,cas=1,num,sign,sum,flag;
//存放棋盘
char vv[maxn][maxn];
//x用-1表示,o用0表示,.用1表示
int v[maxn][maxn];
//visit[i][j]来记录(i,j)点有没有被访问
int visit[maxn][maxn];
//结构体数组来记录每一个o点的位置
struct node
{
    int x,y;
}s[maxn*maxn];
//ss数组记录第i个o点有没有被访问过
int ss[maxn*maxn];

void dfs(int x,int y)
{
    if(sum>=2)
    {
        flag=1;
        return;
    }
    if(flag)
        return;
    for(int i=0;i<4;i++)
    {
        int sx=x+dx[i],sy=y+dy[i];
        if(v[sx][sy]==-1||visit[sx][sy])
            continue;
        if(!visit[sx][sy])
        {
            visit[sx][sy]=1;
            if(v[sx][sy]==1)
                sum++;
            else if(v[sx][sy]==0)
            {
                for(int j=1;j<=num;j++)
                {
                    if(!ss[j]&&sx==s[j].x&&sy==s[j].y)
                    {
                        ss[j]=1;
                        break;
                    }
                }
                dfs(sx,sy);
            }
        }
    }
    if(sum>=2)
    {
        flag=1;
        return;
    }
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        memset(v,-1,sizeof(v));//注意这里初始化为-1,因为棋盘边界上不能放子,默认为x
        memset(visit,0,sizeof(visit));
        memset(s,0,sizeof(s));
        memset(ss,0,sizeof(ss));
        num=0;
        sign=0;
        sum=0;
        flag=0;
        for(int i=1;i<=9;i++)
        {
            scanf("%s",vv[i]+1);
            for(int j=1;j<=9;j++)
            {
                if(vv[i][j]=='x')
                    v[i][j]=-1;
                else if(vv[i][j]=='o')
                {
                    num++;
                    v[i][j]=0;
                    s[num].x=i;
                    s[num].y=j;
                }
                else if(vv[i][j]=='.')
                   v[i][j]=1;
            }
        }

        for(int i=1;i<=num;i++)//对每一个o点进行检索
        {
            sum=0;//sum代表连通块.的个数
            flag=0;
            if(!ss[i])
            {
                memset(visit,0,sizeof(visit));
                visit[s[i].x][s[i].y]=1;
                dfs(s[i].x,s[i].y);
                if(flag==0)
                {
                    sign=1;
                    break;
                }
                ss[i]=1;
            }
        }
        if(sign)
           printf("Case #%d: Can kill in one move!!!\n",cas++);
       else
          printf("Case #%d: Can not kill in one move!!!\n",cas++);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35323001/article/details/53248633