CodeForces 825B(DFS)

问题描述:

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‘,然后去沿四个方向去dfs(),

题目代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
using namespace std;

char mmap[15][15];
bool vis[15][15],ans;
int Next1[2][2]={{-1,0},{1,0}};
int Next2[2][2]={{0,-1},{0,1}};
int Next3[2][2]={{-1,-1},{1,1}};
int Next4[2][2]={{1,-1},{-1,1}};
int cnt;

void dfs1(int x,int y)
{
    if (ans) return ;
    if (cnt==5) {
        ans=true;
        return ;
    }
    for (int i=0;i<2;i++) {
        int dx=x+Next1[i][0];
        int dy=y+Next1[i][1];
        if (dx<1||dx>10||dy<1||dy>10) continue;
        if (mmap[dx][dy]=='.'||mmap[dx][dy]=='0'||vis[dx][dy]) continue;
        if (mmap[dx][dy]=='X') {
            vis[dx][dy]=true;
            cnt++;
            dfs1(dx,dy);
        }
    }

}
void dfs2(int x,int y)
{
    if (ans) return ;
    if (cnt==5) {
        ans=true;
        return ;
    }
    for (int i=0;i<2;i++) {
        int dx=x+Next2[i][0];
        int dy=y+Next2[i][1];
        if (dx<1||dx>10||dy<1||dy>10) continue;
        if (mmap[dx][dy]=='.'||mmap[dx][dy]=='0'||vis[dx][dy]) continue;
        if (mmap[dx][dy]=='X') {
            vis[dx][dy]=true;
            cnt++;
            dfs2(dx,dy);
        }
    }

}
void dfs3(int x,int y)
{
    if (ans) return ;
    if (cnt==5) {
        ans=true;
        return ;
    }
    for (int i=0;i<2;i++) {
        int dx=x+Next3[i][0];
        int dy=y+Next3[i][1];
        if (dx<1||dx>10||dy<1||dy>10) continue;
        if (mmap[dx][dy]=='.'||mmap[dx][dy]=='0'||vis[dx][dy]) continue;
        if (mmap[dx][dy]=='X') {
            vis[dx][dy]=true;
            cnt++;
            dfs3(dx,dy);
        }
    }

}
void dfs4(int x,int y)
{
    if (ans) return ;
    if (cnt==5) {
        ans=true;
        return ;
    }
    for (int i=0;i<2;i++) {
        int dx=x+Next4[i][0];
        int dy=y+Next4[i][1];
        if (dx<1||dx>10||dy<1||dy>10) continue;
        if (mmap[dx][dy]=='.'||mmap[dx][dy]=='0'||vis[dx][dy]) continue;
        if (mmap[dx][dy]=='X') {
            vis[dx][dy]=true;
            cnt++;
            dfs4(dx,dy);
        }
    }

}
int main()
{
    for (int i=1;i<=10;i++) {
        scanf("%s",mmap[i]+1);
    }
    ans=false;
    for (int i=1;i<=10;i++) {
        for (int j=1;j<=10;j++) {
            if (mmap[i][j]=='.'&&!ans) {
                mmap[i][j]='X';
                memset (vis,false,sizeof (vis));
                vis[i][j]=true;
                cnt=1;
                dfs1(i,j);
                memset (vis,false,sizeof (vis));
                vis[i][j]=true;
                cnt=1;
                dfs2(i,j);
                memset (vis,false,sizeof (vis));
                vis[i][j]=true;
                cnt=1;
                dfs3(i,j);
                memset (vis,false,sizeof (vis));
                vis[i][j]=true;
                cnt=1;
                dfs4(i,j);
                mmap[i][j]='.';
            }
            if (ans) break;
        }
        if (ans) break;
    }
    if (ans) puts("YES");
    else puts("NO");
}











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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值