首页 > 代码库 > CodeForces 825B Five-In-a-Row

CodeForces 825B Five-In-a-Row

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

题目大意:5-in-a-row game就是五子棋啦。两个人在一个10×10的棋盘上下五子棋,Alice画×,Bob画○,给出一张他们已经下了的图,现在轮到
Alice下了,问Alice是否会赢。

大致思路:简单模拟遍历棋盘中的每一能走的点,如果这个点所处的行,列或者对角线存在5个连续的×,那么Alice会赢。例如所在的行无法满足要求,那就判断列,以此类推。所在的点如果找不到符合要求的,就换下一个点。详见代码。
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    char a[10][10];
    int i,j,k,l,m,n,flag,cou;
    while(scanf("%s",a[0])==1)
    {
        flag=0;
        for(i=1;i<10;i++)
            scanf("%s",a[i]);
        for(i=0;i<10;i++)
        {
            for(j=0;j<10;j++)
            {
                if(a[i][j]==.)
                {
                    cou=0;//判断开始前都要将计数器归0
                    for(k=j-1;k>=0&&a[i][k]==X;k--)//行变
                        cou++;
                    for(k=j+1;k<10&&a[i][k]==X;k++)
                        cou++;
                    if(cou>=4)
                    {
                        flag=1;
                        cout<<"Yes"<<endl;
                        break;
                    }
                    cou=0;
                    for(l=i-1;l>=0&&a[l][j]==X;l--)//列变
                        cou++;
                    for(l=i+1;l<10&&a[l][j]==X;l++)
                        cou++;
                    if(cou>=4)
                    {
                        flag=1;
                        cout<<"Yes"<<endl;
                        break;
                    }
                    cou=0;
                    for(m=i-1,n=j-1;m>=0&&n>=0&&a[m][n]==X;m--,n--)//"\"方向对角线
                        cou++;
                    for(m=i+1,n=j+1;m<10&&n<10&&a[m][n]==X;m++,n++)
                        cou++;
                    if(cou>=4)
                    {
                        flag=1;
                        cout<<"Yes"<<endl;
                        break;
                    }
                    cou=0;
                    for(m=i-1,n=j+1;m>=0&&n<10&&a[m][n]==X;m--,n++)//"/"方向对角线
                        cou++;
                    for(m=i+1,n=j-1;m<10&&n>=0&&a[m][n]==X;m++,n--)
                        cou++;
                    if(cou>=4)
                    {
                        flag=1;
                        cout<<"Yes"<<endl;
                        break;
                    }
                }
            }
            if(flag)
                break;
        }
        if(!flag)
            cout<<"No"<<endl;
    }
    return 0;
}

 

CodeForces 825B Five-In-a-Row