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 getsthree of his signs in a row next to each other (horizontal, vertical or diagonal).
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.
Print single line: "YES" in case Ilya could have won by making single turn, and "NO" otherwise.
xx.. .oo. x... oox.
YES
x.ox ox.. x.o. oo.x
NO
x..x ..oo o... x.xo
YES
o.x. o... .x.. ooxx
NO
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.
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define inf 0x3f3f3f3f
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int fx[8]= {1,-1,0,0,1,-1,1,-1};
int fy[8]= {0,0,1,-1,1,-1,-1,1};//八个方向
char map[101][101];
int flag;
int dfs()
{
for(int i=1; i<=4; i++)
{
for(int j=1; j<=4; j++)
{
if(map[i][j]=='x')//如果遇见了x就看八个方向上有没有满足条件的
{
for(int k=0; k<8; k++)
{
flag=0;
for(int z=1; z<=2; z++)//同一个方向的两个坐标的位置,如果都为x的话,则证明满足条件
{
int fxx=i+fx[k]*z;//fxx,fyy分别表示与x在同一条直线的坐标,
int fyy=j+fy[k]*z;
if(fxx>=1&&fyy<=4&&fyy>=1&&fxx<=4)
{
if(map[fxx][fyy]=='x') continue;
else flag=1;
}
else
flag=1;
}
if(flag==0)//满足条件返回1
return 1;
}
}
}
}
return 0;
}
int main()
{
//mem(map,'.');
for(int i=1; i<=4; i++)
{
for(int j=1; j<=4; j++)
{
scanf("%c",&map[i][j]);
}
getchar();
}
int ok=0;
for(int i=1; i<=4; i++)
{
for(int j=1; j<=4; j++)
{
//printf("%c",map[i][j]);
if(map[i][j]=='.')
{
map[i][j]='x';//首先将空白变成x,进行搜索
int temp=dfs();
map[i][j]='.';//变回原先的数,以免影响下面的数据
if(temp==1)//如果等一则证明存在这种情况。
{
ok=1;
}
}
}
//printf("\n");
if(ok==1)
break;
}
if(ok==1)//输出结果即可
printf("YES\n");
else
printf("NO\n");
}