Ilya and tic-tac-toe game

Ilya is an experienced player in tic-tac-toe on the4 × 4field. He always starts and plays with Xs. He played a lot of games today with his friend Arseny. The friends became tired and didn't finish the last game. It was Ilya's turn in the game when they left it. Determine whether Ilya could have won the game by making single turn or not.

The rules of tic-tac-toe on the4 × 4field are as follows. Before the first turn all the field cells are empty. The two players take turns placing their signs into empty cells (the first player places Xs, the second player places Os). The player who places Xs goes first, the another one goes second. The winner is the player who first getsthree of his signs in a row next to each other(horizontal, vertical or diagonal).

Input

The tic-tac-toe position is given in four lines.

Each of these lines contains four characters. Each character is '.' (empty cell), 'x' (lowercase English letterx), or 'o' (lowercase English lettero). It is guaranteed that the position is reachable playing tic-tac-toe, and it is Ilya's turn now (in particular, it means that the game is not finished). It is possible that all the cells are empty, it means that the friends left without making single turn.

Output

Print single line: "YES" in case Ilya could have won by making single turn, and "NO" otherwise.

Input

xx..

.oo.

x...

oox.

Output

YES

Input

x.ox

ox..

x.o.

oo.x

Output

NO

Input

x..x

..oo

o...

x.xo

Output

YES

Input

o.x.

o...

.x..

ooxx

Output

NO

这道题是codeforces上面的一道题,大概的题意就是有一个人和他朋友下棋,这个人走x棋,他朋友走o棋,只用走一步,只要这个棋呢三个连成一串(就像五子棋那样连成一串),就赢得胜利。我们要得的解就是这个人能不能赢得胜利,赢的话输出YES,输的话输出NO(都是大写哦)。这道题有两种求解方法,一种是暴力,一种是一种很巧妙地方法(一个很聪明的妹子写出来的)。

暴力的话就枚举每个点,从一个点出发,看它的上下,左右,斜上斜下能不能构成解。

这种巧妙地方法呢,虽然也是枚举每个点,但是和暴力一点都不一样。这种方法是把字符转化成数值从而得到解。我们将x转换成1,o转换成-1, . 转换成0,从一个点出发,如果有一个点满足连续的三个点加在一起的值满足大于1这个条件,则主角胜,否则,失败。

代码如下:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int a[1000];
int maze[100][100];
char s[100];
int dx[]={-1,0,-1,-1},dy[]={-1,-1,0,1};//设置方向
bool check(int i,int j,int k)
{
    return (maze[i][j]+maze[i+dx[k]][j+dy[k]]+maze[i-dx[k]][j-dy[k]])>1;//判断能否成立
}
int main()
{
    memset(maze,-1,sizeof(maze)); 
    int n=4;
    bool flag=false;
    for(int i=1;i<=n;i++)//读数
    {
     scanf("%s",s);
     for(int j=0;j<n;j++)
     {
        if(s[j]=='.')maze[i][j+1]=0;
        else if(s[j]=='x'){
            flag=true;
        maze[i][j+1]=1;}
     }
    }
    for(int i=1;i<=n;i++)
     for(int j=1;j<=n;j++)
      for(int k=0;k<4;k++)
      if(check(i,j,k))
      {
        printf("YES\n");
        return 0;
      }
      
    printf("NO\n");
    return 0;
}

尤其要注意方向的设置不要错了!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Ilya and tic-tac-toe game Ilya is an experienced player i...
    河里的咸鱼阅读 2,325评论 0 0
  • 当扔出钢笔的一刹那,我就后悔了,赶紧捧住女儿的脸,看看她是否受伤了。 感谢上帝,感谢冥冥中所有未知的、令我敬畏的神...
    漠小汐阅读 2,394评论 0 1
  • 马上就2017年了,很多人在说,自己2016年的小目标没有实现。减肥的都被美食宠坏了,赚钱的都没有花的快,学习的书...
    冰淇淋在路上阅读 2,600评论 0 0
  • 以前一直都是从事it测试和研发工作,一直都有想换换工作的想法。可每次投的还是跟原来相关,因为没有从事其他的经验也不...
    雨笛2014阅读 1,267评论 0 1