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

题意:Five-In-a-Row 五子棋游戏。。问下一步放X棋,X会不会获胜。

解题思路:DFS。找到每一个是‘.’的格子(只有‘.’才可以放X呀),依次往这个格子的上、下、左、右、右上、左下、左上、右下搜索,如果连成五颗棋(sum==5)则将标记p变为1(p初始化为0),如果其中有一颗棋不是’X’则break结束这个方向的寻找。

补充:WA了6次…多次测试加上从Codeforce找来的测试数据终于发现错误。。。就是向右上找和向左下找的两个for循环写错了。。。两个for循环要把这个方向上的都搜索完。从tx,ty向右上找完该从tx+1,ty-1向左下找

AC代码:

#include<stdio.h>
#include<string.h>

char mp[10][10];
int p;

void dfs(int x,int y)
{
    int i,j,sum;
    int tx=x;
    int ty=y;
    sum=0;
    for(i=tx;i>=0;i--)
        if(mp[i][ty]!='X') break;else sum++;//向上找 
    for(i=tx+1;i<10;i++)
        if(mp[i][ty]!='X') break;else sum++;//向下找 

    //if(sum>=5) {printf("sum1======%d\n",sum);p=1;}
    if(sum>=5) p=1;
    sum=0;
    for(i=ty;i>=0;i--)
        if(mp[tx][i]!='X') break;else sum++;//向左找 
    for(i=ty+1;i<10;i++)
        if(mp[tx][i]!='X') break;else sum++;//向右找

    //if(sum>=5) {printf("sum2======%d\n",sum); p=1;}
    if(sum>=5) p=1;
    sum=0;
    for(i=tx,j=ty;i>=0&&j>=0;i--,j--)
        if(mp[i][j]!='X') break;else sum++;//向左上找
    for(i=tx+1,j=ty+1;i<10&&j<10;i++,j++)
        if(mp[i][j]!='X') break;else sum++;//向右下找

    //if(sum>=5) {printf("sum3======%d\n",sum);p=1;}
    if(sum>=5) p=1;
    sum=0;
    for(i=tx,j=ty;i>=0&&j<10;i--,j++)
        if(mp[i][j]!='X') break;else sum++;//向右上找
    for(i=tx+1,j=ty-1;j>=0&&i<10;i++,j--)
        if(mp[i][j]!='X') break;else sum++; //向左下找 

    //if(sum>=5) {printf("sum4======%d\n",sum);p=1;}
    if(sum>=5) p=1;
}

int main()
{
    int i,j;
    for(i=0;i<10;i++)
        scanf("%s",mp[i]);
    p=0;
    for(i=0;i<10;i++)
    {
        for(j=0;j<10;j++)
        {
            if(mp[i][j]=='.')
            {
                mp[i][j]='X';
                dfs(i,j);
                mp[i][j]='.';
            }
        }
    }
    if(p) printf("YES\n");
    else printf("NO\n"); 
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值