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'.
Example
Input
XX.XX.....
.....OOOO.
..........
..........
..........
..........
..........
..........
..........
YES
Input
XXOXX.....
OO.O......
..........
..........
..........
..........
..........
..........
..........
..........
Output
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'.
Example
Input
XX.XX.....
.....OOOO.
..........
..........
..........
..........
..........
..........
..........
..........
OutputYES
Input
XXOXX.....
OO.O......
..........
..........
..........
..........
..........
..........
..........
..........
Output
NO
题意
Alice与Bob在下10*10的五子棋,Alice用的是‘X’,Bob用的是‘O’,判断Alice在下一步能否成功(即五个‘X’相连),可以横着相连,竖着相连,斜着相连,在这几种
相连方式中若有五个‘X’相连,则输出YES,否则输出NO。
题解
本题可用暴力的方法来实现,因为是‘.’的时候才可以下棋,所以从‘.’开始找,如果横着找到存在>=4个‘X’(相连的),输出YES,横着分左边和右边两个方向,左边和右边找到的相连的‘X’一共大于等于4,就会输出YES。同理竖着和斜着的时候也都是这样找的,如果在一种情况下找到相连的‘X’大于等于4,则输出YES,否则,每种情况下找到的
‘X’都清零,才能进行下一种方式,最终循环结束后,如果没找到,就输出NO。
#include<stdio.h>
#include<string.h>
char a[10][10];
int flag;
int main()
{
int i,j,k,s,e,l,f,w,sum;
while(~scanf("%s",a[0]))
{
flag=0;
for(i=1; i<10; i++)
scanf("%s",a[i]);
for(i=0; i<10; i++) //横坐标
{
for(j=0; j<10; j++) //纵坐标
{
if(a[i][j]=='.') //因为是'.'的时候才可以走
{
sum=0; //sum赋初值
for(k=j+1; k<10&&a[i][k]=='X'; k++) //横向找,'.'的右边
sum++;
for(k=j-1; k>=0&&a[i][k]=='X'; k--) //横向找,'.'的左边
sum++;
if(sum>=4) //满足题意
{
flag=1;
printf("YES\n");
break;
}
sum=0; //如果不满足,进行下一种方式,sum为0
for(s=i+1; s<10&&a[s][j]=='X'; s++) //竖着找,'.'的下边
sum++;
for(s=i-1; s>=0&&a[s][j]=='X'; s--) //竖着找,'.'的上边
sum++;
if(sum>=4)
{
flag=1;
printf("YES\n")
break;
}
sum=0;
for(e=i+1,f=j+1; e<10&&f<10&&a[e][f]=='X'; e++,f++) //向右下找
sum++;
for(e=i-1,f=j-1; e>=0&&f>=0&&a[e][f]=='X'; e--,f--) //向右上找
sum++;
if(sum>=4)
{
flag=1;
printf("YES\n");
break;
}
sum=0;
for(l=i+1,w=j-1; l<10&&w>=0&&a[l][w]=='X'; l++,w--) //向左下找
sum++;
for(l=i-1,w=j+1; l>=0&&w<10&&a[l][w]=='X'; l--,w++) //向左上找
sum++;
if(sum>=4)
{
flag=1;
printf("YES\n");
break;
}
}
}
if(flag==1)
break;
}
if(flag==0) //没找到的时候
printf("NO\n");
}
return 0;
}