Five-In-a-Row
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.
..........
..........
..........
..........
..........
..........
..........
..........
Output
YES
Input
XXOXX.....
OO.O......
..........
..........
..........
..........
..........
..........
..........
..........
Output
NO
题意:这是一个五子棋游戏,X是你的棋子,O是对方棋子,.是空格,现在你在空格处下一子,判断是否能成五子,能成则输出
YES,否则输出NO。思路:开始想了很久,都没有思路,后来突然想到我的课程设计——五子棋游戏,查看一下才写出来的,不过也不相同,具体的代码中有注释。
#include<stdio.h>
#include<string.h>
char map[20][20];
int dis[4][4]= {{0,1,0,-1},{1,0,-1,0},{1,1,-1,-1},{-1,1,1,-1}};
int win(int x,int y) //dis数组存的是方向共有8个方向,但可以说是四个主方向
{
int a=x,b=y,n=0,m=0;
while(n+1<5) //这个循环为四,表示主方向
{
m=0;
a=x;
b=y;
while(map[a][b]=='X') //从(x,y)一点开始一个方向循环
{
m++; //m表示X棋子的个数
a=a+dis[n][0]; //方向延伸
b=b+dis[n][1];
if(m==5)
return 1;
if(a<0||b<0||a>=10||b>=10)//过界,停止
break;
} //再从(x,y)点循环,和上一个方向相反
m=m-1; //(x,y)点循环两次,即m-1;
a=x;
b=y;
while(map[a][b]=='X')
{
m++;
a=a+dis[n][2];
b=b+dis[n][3];
if(m==5)
return 1;
if(a<0||b<0||a>=10||b>=10)
break;
}
n++; //两个while循环,方向相反 也即一个主方向。
}
return 0;
}
int main()
{
while(~scanf("%s",&map[0]))
{
for(int i=1; i<10; i++)
scanf("%s",map[i]);
int flag=0,k;
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
if(map[i][j]=='.')
{
map[i][j]='X'; //如果下一子下在此处,
k=win(i,j); //调用判断是否能赢
if(k)
{
flag=1;
break;
}
map[i][j]='.'; //不能赢悔棋。
}
if(flag)
break;
}
if(flag)
printf("YES\n");
else printf("NO\n");
}
return 0;
}