寒假训练营最后测试一:B题

B - 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’.


Examples


在这里插入图片描述
在这里插入图片描述
题解:现在我最想说的是,后悔以前没学好英语,开始上午的时候看了下题目觉得是道难题,以为要到整个棋盘都满,我当时就一脸懵逼
原来只需要判断当Alice再下一步棋后她是否会赢!!!!!!!!!!!!!
于是我便选择直接暴力解决,看能不能实现
开始由于不太强,很多细节都没注意,导致案例始终都没过,原来是太蠢了,出现了考虑左边的时候没考虑右边也是一边线的问题,解决完这些问题后,凭着试试的运气交了一下,没想到暴力居然还AC了
代码如下:

#include<cstdio>
#include<cstring>
int main()
{
    char Map[15][15];
    for(int i=0;i<10;i++)
    {
        scanf("%s",Map[i]);
    }
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<10;j++)
        {
            if(Map[i][j]=='.')
            {
                int s;
                s=1;
                for(int k=j-1;k>=0;k--)
                {
                    if(Map[i][k]=='X')
                    {
                        s++;
                        if(s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else
                        break;
                }
                for(int k=j+1;k<10;k++)
                {
                    if(Map[i][k]=='X')
                    {
                        s++;
                        if(s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else
                        break;
                }
                s=1;
                for(int k=i-1;k>=0;k--)
                {
                    if(Map[k][j]=='X')
                    {
                        s++;
                        if(s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else
                        break;
                }
                for(int k=i+1;k<10;k++)
                {
                    if(Map[k][j]=='X')
                    {
                        s++;
                        if(s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else
                        break;
                }
                s=1;
                for(int p=i-1,q=j-1;p>=0,q>=0;p--,q--)
                {
                    if(Map[p][q]=='X')
                    {
                        s++;
                        if(s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else
                        break;
                }
                for(int p=i+1,q=j+1;p<10,q<10;p++,q++)
                {
                    if(Map[p][q]=='X')
                    {
                        s++;
                        if(s==5)
                        {
                            printf("YES\n");
                            return 0;
                        };
                    }
                    else
                        break;
                }
                s=1;
                for(int p=i-1,q=j+1;p>=0,q<10;p--,q++)
                {
                    if(Map[p][q]=='X')
                    {
                        s++;
                        if(s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else
                        break;
                }
                for(int p=i+1,q=j-1;p<10,q>=0;p++,q--)
                {
                    if(Map[p][q]=='X')
                    {
                        s++;
                        if(s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else
                        break;
                }
            }
        }
    }
    printf("NO\n");
    return 0;
}

小白心得

猜你喜欢

转载自blog.csdn.net/boliu147258/article/details/87824377
今日推荐