CodeForces 825B(DFS)

来源:互联网 发布:linux配置ip地址命令 编辑:程序博客网 时间:2024/06/06 08:43

问题描述:

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");}











原创粉丝点击