2015ccpc——G

来源:互联网 发布:软件质量指标 编辑:程序博客网 时间:2024/06/10 19:50
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
Sample Output
Case #1: Can kill in one move!!!

Case #2: Can not kill in one move!!!

题意:再放一个x,问能不能将o包围起来。

解题思路:判断是一个节点被围起来,还是一个快被围起来。单个节点比较好判断,只需要判断o的上下左右'.'的个数,个数为1,则表示可以成功。

对于块,就当成联通块去处理,看这个联通块的'.'的个数。


#include <iostream>#include <cstdio>#include <cstring>using namespace std;char maze[10][10];int vis1[10][10],vis2[10][10];int dx[]={1,-1,0,0},dy[]={0,0,1,-1};int cnt;bool check1(int x,int y){    if(x<0||x>=9||y<0||y>=9||maze[x][y]!='.')        return false;    return true;}bool check2(int x,int y){    if(x<0||x>=9||y<0||y>=9||maze[x][y]!='o')        return false;    return true;}void dfs(int x,int y){    int x1,y1;    for(int i=0;i<4;i++)//处理单个节点    {        x1=x+dx[i];        y1=y+dy[i];        if(check1(x1,y1)&&!vis2[x1][y1])        {            vis2[x1][y1]=1;            cnt++;        }    }    for(int i=0;i<4;i++)//处理联通块    {        x1=x+dx[i];        y1=y+dy[i];        if(check2(x1,y1)&&!vis1[x1][y1])        {            vis1[x1][y1]=1;            dfs(x1,y1);        }    }}int main(){    int t,cas=1,flag;    scanf("%d",&t);    while(t--)    {        flag=0;        for(int i=0;i<9;i++)            scanf("%s",maze[i]);        memset(vis1,0,sizeof(vis1));        memset(vis2,0,sizeof(vis2));        for(int i=0;i<9;i++)        {            for(int j=0;j<9;j++)            {                if(maze[i][j]=='o'&&!vis1[i][j])                {                    vis1[i][j]=1;                    memset(vis2,0,sizeof(vis2));                    cnt=0;                    dfs(i,j);                    if(cnt==1)//表明只有一个地方恰好可以放x。                    {                        flag=1;                    }                    if(flag)                        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;}


原创粉丝点击