大致题意:题意很简单,就是两个人在下五子棋,一个人的棋子是X,另一个人的棋子是O,现在轮到X了,问能否在“ . ”区域放一个棋子使得在八个方向上的任意一个方向上能构成五个或者五个以上的连续X棋子,如果可以输出YES,否则输出NO
大致思路:一开始想的用dfs,但是想的太复杂了,没有做出来,最后看了队友的思想,直接暴力八个方向,因为棋盘区域才是10X10的,完全没问题,代码也很简单
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.
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.
Print 'YES' if it's possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print 'NO'.
XX.XX..... .....OOOO. .......... .......... .......... .......... .......... .......... .......... ..........
YES
XXOXX..... OO.O...... .......... .......... .......... .......... .......... .......... .......... ..........
NO
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char map[12][12];
int n;
void dfs(int x,int y)
{
int xx,yy,sum,i,j;
xx=x;
yy=y;
sum=0;
//下面是扩展八个方向,有一个方向满足连续五个棋子就可以输入YES
for(i=xx; i>=0; i--)
{
if(map[i][yy]!='X')
break;
else
sum++;
}
for(i=xx+1; i<10; i++)
{
if(map[i][yy]!='X')
break;
else
sum++;
}
if(sum>=5)n=1;
sum=0;
for(i=yy; i>=0; i--)
{
if(map[xx][i]!='X')
break;
else
sum++;
}
for(i=yy+1; i<10; i++)
{
if(map[xx][i]!='X')
break;
else
sum++;
}
if(sum>=5)n=1;
sum=0;
for(i=xx,j=yy; i>=0&&j>=0; i--,j--)
{
if(map[i][j]!='X')
break;
else
sum++;
}
for(i=xx+1,j=yy+1; i<10&&j<10; i++,j++)
{
if(map[i][j]!='X')
break;
else
sum++;
}
if(sum>=5)n=1;
sum=0;
for(i=xx,j=yy; i>=0&&j<10; i--,j++)
{
if(map[i][j]!='X')
break;
else
sum++;
}
for(i=xx+1,j=yy-1; i<10&&j>=0; i++,j--)
{
if(map[i][j]!='X')
break;
else
sum++;
}
if(sum>=5)n=1;
}
int main()
{
int i,j;
for(i=0; i<10; i++)
scanf("%s",map[i]);
n=0;
for(i=0; i<10; i++)
for(j=0; j<10; j++)
{
if(map[i][j]=='.')
{
map[i][j]='X'; //标记
dfs(i,j);
map[i][j]='.'; //深搜回溯
}
}
if(n==1)printf("YES\n");
else
printf("NO\n");
return 0;
}