Five-In-a-Row CodeForces

来源:互联网 发布:不履行网络安全法 编辑:程序博客网 时间:2024/06/07 18:50

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'.

Example
Input
XX.XX..........OOOO.................................................................................
Output
YES
Input
XXOXX.....OO.O......................................................................................
Output

NO


题意:

下五子棋,判断在10*10的棋盘上再放入一个子,即“X”,是否能得到五个连续的棋子。

题解:

遍历图,找到“.”,将其替换为“X”,尝试其八种方向,每两个相反的方向得到的棋子个数相加。如果满足五个,返回。如果不满足,尝试所有方向。注意有大于五个的可能。搜索之后把“.”替换回来。


代码如下:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;char mp[11][11];int p;void dfs(int x,int y){    int tx=x;    int ty=y;    int i,j;    int sum=0;    sum=0;    for(i=tx; i>=0; i--)        if(mp[i][ty]=='X')            sum++;        else            break;    for(i=tx+1; i<10; i++)        if(mp[i][ty]=='X')            sum++;        else            break;    if(sum>=5)        p=1;    sum=0;    for(i=tx+1,j=ty+1; i<10,j<10; i++,j++)        if(mp[i][j]=='X')            sum++;        else            break;    for(i=tx,j=ty; i>=0,j>=0; i--,j--)        if(mp[i][j]=='X')            sum++;        else            break;    if(sum>=5)        p=1;    sum=0;    for(i=ty+1; ty<10; i++)        if(mp[tx][i]=='X')            sum++;        else            break;    for(i=ty; i>=0; i--)        if(mp[tx][i]=='X')            sum++;        else            break;    if(sum>=5)        p=1;    sum=0;    for(i=tx+1,j=ty-1; i<10,j>=0; i++,j--)        if(mp[i][j]=='X')            sum++;        else            break;    for(i=tx,j=ty; i>=0,j<10; i--,j++)        if(mp[i][j]=='X')            sum++;        else            break;    if(sum>=5)        p=1;    sum=0;}int main(){    int i,j;    for(i=0; i<10; i++)        scanf("%s",mp[i]);    for(i=0; i<10; i++)    {        for(j=0; j<10; j++)            if(mp[i][j]=='.')            {                p=0;                mp[i][j]='X';                dfs(i,j);                mp[i][j]='.';                if(p)                    break;            }        if(p)            break;    }    if(p)        printf("YES\n");    else        printf("NO\n");}