1. 程式人生 > >hdu 5546 Ancient Go

hdu 5546 Ancient Go

題目連結: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;
}