B. Five-In-a-Row

来源:互联网 发布:linux 查看caffe版本 编辑:程序博客网 时间:2024/06/05 17:13
B. Five-In-a-Row
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice and Bob play 5-in-a-row game. They have a playing field of size 10 × 10. In turns they put either crosses or noughts, one at a time. Alice puts crosses and Bob puts noughts.

In current match they have made some turns and now it's Alice's turn. She wonders if she can put cross in such empty cell that she wins immediately.

Alice wins if some crosses in the field form line of length not smaller than 5. This line can be horizontal, vertical and diagonal.

Input

You are given matrix 10 × 10 (10 lines of 10 characters each) with capital Latin letters 'X'being a cross, letters 'O' being a nought and '.' being an empty cell. The number of 'X'cells is equal to the number of 'O' cells and there is at least one of each type. There is at least one empty cell.

It is guaranteed that in the current arrangement nobody has still won.

Output

Print 'YES' if it's possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print 'NO'.

Examples
input
XX.XX..........OOOO.................................................................................
output
YES
input
XXOXX.....OO.O......................................................................................
output
NO
 

 

题意:两个人下棋,轮到你下了,你想知道你一步是否能够赢棋,可以一步赢的话,输出YES,否则输出NO。

解题思路:首先我们需要从X的地方开始,然后朝着八个方向一次查,一旦连够五子,立马跳出循环,这题需用深搜做,这是生活中五子棋的一样。

代码:

#include<stdio.h>

#include<string.h>

charmap[20][20];

intd[4][4]= {{0,1,0,-1},

    {1,0,-1,0},

    {1,1,-1,-1},

    {-1,1,1,-1}

};

intp(int x,int y)//X的话进入函数

{

    int a=x,b=y,n=0,m=0;

    while(n+1<5)//n==0)横着找(n==1)竖着找(n==2)右斜找(n==3)左斜找

    {

        m=0;

        a=x;

        b=y;

        while(map[a][b]=='X')//X的话继续朝着一个方向找,一直到循环结束

        {

            m++;//当前的连续的X的个数

            a+=d[n][0];//横右。竖下。斜右下。斜右上方向走

            b+=d[n][1];

            if(m==5)

                return 1;

           if(a<0||b<0||a>=10||b>=10)//即不在棋盘范围内

                break;

        }

        m=m-1;//这个容易忽略掉,因为你像一个固定方向查过棋子后,又回到原点,然后在从新对未查过的方向找,

        //都是从mapa】【b】点开始找的,所以重服了一次

        a=x;

        b=y;

        while(map[a][b]=='X')

        {

            m++;

            a=a+d[n][2];//横左。竖上。斜左下。斜左上方向走

            b=b+d[n][3];

            if(m==5)//五子连珠,游戏结束

                return 1;

           if(a<0||b<0||a>=10||b>=10)//即不在棋盘范围内

                break;

        }

        n++;//n控制的是每次找的方向(n==0)横(n==1)竖(n==2)右斜

    }

    return 0;

}

intmain()

{

    while(~scanf("%s",&map[0]))

    {

        for(int i=1; i<10; i++)

            scanf("%s",map[i]);

        int flag=0,k;

        for(int i=0; i<10; i++)//把棋盘中每一个空地都尝试下,找到最有价值的点

        {

            for(int j=0; j<10; j++)

                if(map[i][j]=='.')//尝试

                {

                    map[i][j]='X';//先变为X,进入连棋函数,看是否能够赢

                    k=p(i,j);

                    map[i][j]='.';//还变为空地,因为我们是尝试,不一定成功

                    if(k)

                    {

                        flag=1;//标记为1

                        break;

                    }

                }

            if(flag)//成功的话停止查找

                break;

        }

        if(flag)

            printf("YES\n");

        else

            printf("NO\n");

    }

    return 0;

}

 


原创粉丝点击