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

题意:
多组输入,五子棋的规则。给一个10*10的棋盘,棋盘内有两种棋子“X”和“O”,现在要再加入一个棋子“X”,使得棋子“X”可以按照五子棋的规则连成五个或五个以上。如果加入一个可以连成的话输出“YES”,不能的话输出“NO”。

题解:向8个方向模拟,判断是否可行。(分为4个主要方向)

AC代码:


#include<stdio.h>  
#include<string.h>  
char map[10][10];
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)              
{
int xl = x, yl = y, n = 0, m = 0;
while (n + 1<5)             
{
m = 0;
xl = x;
yl = y;
while (map[xl][yl] == 'X')   
{
m++;               
xl = xl + dis[n][0];    
yl = yl + dis[n][1];
if (m == 5)
return 1;
if (xl<0 || yl<0 || xl >= 10 || yl >= 10)//过界,停止  
break;
}                   
m = m - 1;             
xl = x;
xl = y;
while (map[xl][yl] == 'X')
{
m++;
xl = xl + dis[n][2];
yl = yl + dis[n][3];
if (m == 5)
return 1;
if (xl<0 || yl<0 || xl >= 10 || yl >= 10)//过界,停止
break;
}
n++;                    
}
return 0;
}
int main()
{
while (~scanf("%s", map[0]))
{
for (int i = 1; i<10; i++)
scanf("%s", map[i]);
int flag = 0;
for (int i = 0; i<10; i++)
{
for (int j = 0; j<10; j++)
{
if (map[i][j] == '.')
{
map[i][j] = 'X';          
          
if (win(i, j))
{
flag = 1;
break;
}
map[i][j] = '.';      
}
}
if (flag)
break;
}
if (flag)
printf("YES ");
else printf("NO ");
}
return 0;
}



原文地址:https://www.cnblogs.com/csushl/p/9386630.html