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
#include
#include
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

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

你可能感兴趣的:(Ilya and tic-tac-toe game)