CodeForce 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
      这个题猛一看so easy。但仔细一看又有些力不从心。虽说是一道搜索题,但模式有些难想。实际上感觉模拟一下效果更好,只不过代码。。。。好长。

思路其实很简单,每找到一个“.”,就进行上、下、左、右、左下、右上、左上、右下八个方向的判断。每进行一次对应的两个方向,要重新将个数s变为一。

虽然方向很多。。但其实变得并不少,复制粘贴几次就好啦,需要改的只是for循环里的。还有这道题不一定要进行多次输入,题里没有说明。

代码如下:

#include<stdio.h>
char mp[12][12];
int main()
{
    for (int i = 0; i < 10; ++i)
    {
        scanf("%s",mp[i]);
    }

    for (int i = 0; i < 10; ++i)
    {
        for (int j = 0; j < 10; ++j)
        {
            if (mp[i][j] == '.')          //每找到一个.都要搜。
            {
                int s=1;
                // 向左
                s = 1;
                for (int k = j - 1; k >= 0; --k)    //以后这些要改
                {
                    if (mp[i][k] == 'X')
                    {
                        ++s;
                        if (s==5)
                        {
                            printf("YES\n");    //当搜到5个时就一定赢了,所以没有搜的必要了。
                            return 0;          //提前结束程序,也可以标记一下,不过有些麻烦
                        }
                    }
                    else break;
                }
                // 向右
                for (int k = j + 1; k < 10; ++k)
                {
                    if (mp[i][k] == 'X')           //if里面的都不用改啦,都是判断。
                    {
                        ++s;
                        if (s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }
                // 向上  
                s = 1;                           //这里要重新变为1.因为是从中间往两边搜的,上面已经搜了横着的(左右),s已经改变,再搜上下,要重新赋值。
                for (int k = i - 1; k >= 0; --k)
                {
                    if (mp[k][j] == 'X')
                    {
                        ++s;
                        if (s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }

                // 向下
                for (int k = i + 1; k < 10; ++k)
                {
                    if (mp[k][j] == 'X')
                    {
                        ++s;
                        if (s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }
                // 左上
                s = 1;
                for (int k = i - 1, q = j - 1; k >= 0 && q >= 0; --k, --q)  //这里需要多定义一个变量,因为斜着搜,两个方向都会改变
                {
                    if (mp[k][q] == 'X')
                    {                              //果断复制粘贴^_^
                        ++s;
                        if (s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }

                // 右下
                for (int k = i + 1, q = j + 1; k < 10 && q < 10; ++k, ++q)  //同上
                {
                    if (mp[k][q] == 'X')
                    {
                        ++s;
                        if (s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }
                // 左下
                s = 1;
                for (int k = i + 1, q = j - 1; k < 10 && q >= 0; ++k, --q)
                {
                    if (mp[k][q] == 'X')
                    {
                        ++s;
                        if (s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }
                // 右上
                for (int k = i - 1, q = j + 1; k >= 0 && q < 10; --k, ++q)
                {
                    if (mp[k][q] == 'X')
                    {
                        ++s;
                        if (s==5)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }
            }
        }
    }
    printf("NO\n");             //如果搜完了所有的点还没有Yes,那就只有NO啦。
    return 0;                     //搜索也是这样的,这个代码只不过模仿了搜索的过程
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值