Educational Codeforces Round 25 B. Five-In-a-Row

题目链接:http://codeforces.com/contest/825/problem/B

 

B. Five-In-a-Row

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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
Input
XX.XX.....
.....OOOO.
..........
..........
..........
..........
..........
..........
..........
..........

Output

YES

Input

XXOXX.....
OO.O......
..........
..........
..........
..........
..........
..........
..........
..........

Output

NO

题意:

找到一个'.',将它改成'X'后,能否出现5子连线.

题解:

因为棋盘是10*10,所以直接暴力深搜即可,标记做好,然后判断是否符合题意.

代码:

#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
#define maxn 15
char str[maxn][maxn];
bool vis[maxn][maxn],ans;
int dirx[8]={0,0,-1,1,1,-1,1,-1,},diry[8]={-1,1,0,0,1,-1,-1,1,};
bool valid(int x,int y)
{
    if(x>=0&&x<10&&y>=0&&y<10)  return true;
    else return false;
}
void dfs(int x,int y,int lx,int ly,int cnt,bool flag)
{
    vis[x][y]=true;
    if(cnt>=4)  {ans=true;  return ;}
    bool FLAG=false;
    if(str[x][y]=='.')  flag=true;
    if(lx==-2&&ly==-2)  FLAG=true;
    for(int i=0;i<8;i++)
    {
        int nx=dirx[i]+x,ny=diry[i]+y;
        if(FLAG)   {lx=dirx[i],ly=diry[i];}
        if(valid(nx,ny)&&!vis[nx][ny]&&str[nx][ny]=='X'&&dirx[i]==lx&&diry[i]==ly)
            dfs(nx,ny,dirx[i],diry[i],cnt+1,flag);
        else if(valid(nx,ny)&&!vis[nx][ny]&&str[nx][ny]=='.'&&!flag&&dirx[i]==lx&&diry[i]==ly)
            dfs(nx,ny,dirx[i],diry[i],cnt+1,1);
    }
}
int main()
{
    for(int i=0;i<10;i++)  scanf("%s",str[i]);
    for(int i=0;i<10;i++)
        for(int j=0;j<10;j++)
        {
            memset(vis,false,sizeof(vis));
            if(str[i][j]=='X'||str[i][j]=='.')  dfs(i,j,-2,-2,0,0);
        }
    if(ans)  puts("YES");
    else puts("NO");
    return 0;
}
原文地址:https://www.cnblogs.com/weimeiyuer/p/7204255.html