Cdoeforces 754 B - Ilya and tic-tac-toe game

来源:互联网 发布:舆情指数算法 编辑:程序博客网 时间:2024/05/22 13:15

Ilya is an experienced player in tic-tac-toe on the 4 × 4 field. 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 the 4 × 4 field 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 gets three 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 letter x), or 'o' (lowercase English letter o). 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.

Example
Input
xx...oo.x...oox.
Output
YES
Input
x.oxox..x.o.oo.x
Output
NO
Input
x..x..ooo...x.xo
Output
YES
Input
o.x.o....x..ooxx
Output
NO

Note


In the first example Ilya had two winning moves: to the empty cell in the left column and to the leftmost empty cell in the first row.

In the second example it wasn't possible to win by making single turn.

In the third example Ilya could have won by placing X in the last row between two existing Xs.

In the fourth example it wasn't possible to win by making single turn.



题目大意:

  一个4*4的矩阵,然后下棋,如果三个相同标识符在一串(横竖或者斜着)就算赢,现在是x和o,轮到x下,“.”表示x可以下这个地方,一步,如果这一步能串三个x就算赢。


题目分析:

  直接暴力。就是有一点,判断条件有点多,横竖加上斜着,最主要的是斜着,特别容易搞漏掉,我wrong 了两次就是因为这个。

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;char map[10][10];char s[10];int flag=0;void dfs(){for(int i=1;i<=4;i++){for(int j=1;j<=4;j++){  if(map[i][j]=='x'&&map[i+1][j]=='x'&&map[i+2][j]=='x'){  flag=1;  return;  }  if(map[i][j]=='x'&&map[i][j+1]=='x'&&map[i][j+2]=='x'){  flag=2;  return;  }  if(map[i][j]=='x'&&map[i+1][j+1]=='x'&&map[i+2][j+2]=='x'){  flag=3;  return;  }  if(map[i][j]=='x'&&map[i-1][j-1]=='x'&&map[i-2][j-2]=='x'){  flag=3;  return;  }  if(map[i][j]=='x'&&map[i-1][j+1]=='x'&&map[i-2][j+2]=='x'){  flag=3;  return;  }  if(map[i][j]=='x'&&map[i+1][j-1]=='x'&&map[i+2][j-2]=='x'){  flag=3;  return;  }}}return ;}int main(){int n,x,m=0;memset(map,0,sizeof(map));for(int i=1;i<=4;i++){scanf("%s",s);for(int j=0;j<=3;j++)  map[i][j+1]=s[j];}/*/for(int i=1;i<=4;i++){for(int j=1;j<=4;j++){          printf("%c",map[i][j]);}printf("\n");}/*/for(int i=1;i<=4;i++){for(int j=1;j<=4;j++){m=0;if(map[i][j]=='.'&&flag==0){map[i][j]='x';dfs();m=1;}if(m==1)  map[i][j]='.';}if(flag==1)break;}if(flag>=1)      printf("YES\n",flag);else printf("NO\n");    /*/for(int i=1;i<=4;i++){for(int j=1;j<=4;j++){          printf("%c",map[i][j]);}printf("\n");}/*/    return 0;}

0 0
原创粉丝点击