Five in a Row
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 310 Accepted Submission(s): 134
Problem Description
The ACM Team of WHU is planning an online AI arena for Go-bang (Five in a Row). Now, we need some help.
We all know the famous game "Five in a Row". It is traditionally played with go pieces (black and white stones) on a go
board (15x15 intersections). The winner is the first player to get an unbroken row of five stones horizontally, vertically, or
diagonally.
We now have an uncompleted game, that is to say, yet no one won. You are required to tell us that whether the next player
could win within one step or not.
We all know the famous game "Five in a Row". It is traditionally played with go pieces (black and white stones) on a go
board (15x15 intersections). The winner is the first player to get an unbroken row of five stones horizontally, vertically, or
diagonally.
We now have an uncompleted game, that is to say, yet no one won. You are required to tell us that whether the next player
could win within one step or not.
Input
The input consists of multiple test cases. The first line of input contains an integer T, which is the number of test cases.
Each test case is a 15 * 15 go board, 'B' represent for a black stone while 'W' for white ones. '.' represents cross with no
stone represented by 0.
[Technical Specification]
T is an integer, and T <= 20.
The number of white stones is either equal or 1 less than the number of black stones.
There is at least ONE '.' in each test case.
Test cases are separated by ONE empty line.
You can assume that black player goes first.
Each test case is a 15 * 15 go board, 'B' represent for a black stone while 'W' for white ones. '.' represents cross with no
stone represented by 0.
[Technical Specification]
T is an integer, and T <= 20.
The number of white stones is either equal or 1 less than the number of black stones.
There is at least ONE '.' in each test case.
Test cases are separated by ONE empty line.
You can assume that black player goes first.
Output
For each test case, print a single line containing the result. If the next player could win within one step, display 'YES',
otherwise, display 'NO'.
otherwise, display 'NO'.
Sample Input
2 ........W...... ..W......W...B. .W....B........ .W.......W..... ..BB....W...... ...B........... ....B........WW ...........B... ....W.......... .W..B.B....WBB. BW........B.W.B ............... W.....BB.W....W B..B.W.BB....B. ........WW..... ...B...W..BBW.. W..WBW..W...B.. ...W..WBW...... .....BWW..B.B.. ..BW.B.B..W.... B..W..WBW...... B......B......W B.B...B....W... ....B.WB...B... B.W............ .....WW...B.... ..B.....B...W.W .W.........WBW. B.............. ..W.B...W..W..B
Sample Output
NO YES
先判断要走哪种棋子,然后对能下的地方枚举4对相反方向看能不能连成五子
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
#include <vector>
using namespace std;
char mapp[20][20];
const int N = 15;
/*八对相反的位置:
(-1,0) (1,0) 上 下
(0,-1) (0,1) 左 右
(-1,-1) (1,1) 左上 右下
(-1,1) (1,-1) 右上 左下
*/
int dir[4][4] = { -1, 0, 1, 0, 0, -1, 0, 1, -1, -1, 1, 1, -1, 1, 1, -1 };
//判断是否出棋盘
bool Isout(int x, int y) {
if (x < 0 || x >= N || y < 0 || y >= N) {
return true;
}
return false;
}
/*五子相连只有三种情况:
XOOOO,OOOOX,OOXOO
在最左加一子,在最右面加一子,在中间某个地方加一子
然后在上面4对相反的方向上枚举尝试
*/
bool check(int x, int y, char go) {
int cou;
for (int i = 0; i < 4; i++) {
cou = 1; //要填的棋子占一个
//方向A
for (int nx = x + dir[i][0], ny = y + dir[i][1]; !Isout(nx, ny) && mapp[nx][ny] == go; nx += dir[i][0], ny += dir[i][1]) {
cou++;
}
if (cou == 5) break; //对应XOOOO成功
//与方向A反向的反向B
for (int nx = x + dir[i][2], ny = y + dir[i][3]; !Isout(nx, ny) && mapp[nx][ny] == go; nx += dir[i][2], ny += dir[i][3]) {
cou++;
}
if (cou == 5) break; //如果第一个循环没有进入的话,对应OOOOX成功
//如果第一个循环进入了的话,则说明两个方向合在一起成功,对应OOXOO成功
}
if (cou == 5) return true;
return false;
}
int main()
{
int T;
scanf("%d", &T);
while (T--) {
int cw, cb; //记录黑白子的数量
cw = cb = 0;
for (int i = 0; i < N; i++) {
scanf("%s", mapp[i]);
for (int j = 0; j < N; j++) {
if (mapp[i][j] == 'W') cw++;
if (mapp[i][j] == 'B') cb++;
}
}
/*题目要求第一步一定是B,并且cw <= cb
如果cw < cb说明下一步该白子走
如:
B W B ->W
如果cw == cb说明下一步该黑子走
如:
B W B W ->B
*/
char next = cw < cb ? 'W' : 'B';
//标记是否有五子相连
bool flag = false;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (mapp[i][j] == '.' && check(i, j, next)) {
flag = true;
break;
}
}
if (flag) break;
}
if (flag) {
puts("YES");
}
else {
puts("NO");
}
}
return 0;
}