hiho一下 第204周 Five in a Row

Five in a Row

  • 描述

Five in a Row is a game played on a 15x15 go board. Black and White take turns to place a stone of their color in an empty spot. The winner is the first player to get an unbroken row of five successive stones horizontally, vertically, or diagonally.

Given a Five in a Row game board, your task is to work out which color wins. Note that the input board may not be valid. It is possible that none or both colors have five successive stones.

  • 输入

The first line contains an integer T, the number of test cases. (1 <= T <= 10)

For each test case there is an 15x15 matrix denoting the board. ‘B’ indicates a black stone, ‘W’ indicates a white stone and ‘.’ indicates and empty spot.

  • 输出

For each test case output “Black”, “White”, “None” or “Both” in a separate line denoting which color(s) have five successive stones.

  • 样例输入
2
...............
...............
...............
...............
........B......
......BWW......
.....BWBW......
.....WBBW......
....WBBBBW.....
...W...BW......
......WWB......
......B........
...............
...............
...............
...............
.W.............
..W............
...W...B.......
....W.B........
.....B.........
....B.W........
...B...W.......
........W......
.........W.....
..........W....
...........W...
...............
...............
...............
  • 样例输出
    White
    Both
#define _CRT_SECURE_NO_WARNINGS   //防止scanf告警
#include <iostream>
using namespace std;
const int fx[4] = { 1, 0, 1, -1 };
const int fy[4] = { 0, 1, 1, 1 };
char a[20][20];
int main() {
    int t;
    const int n = 15;
    scanf("%d", &t);
    while (t--) {
        for (int i = 0; i < n; i++) scanf("%s", a[i]);
        int tb = 0, tw = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (a[i][j] == '.') continue;
                for (int k = 0; k < 4; k++) {
                    int x, y, flag = 0;
                    for (int h = 0; h < 5; h++) {
                        x = i + fx[k] * h;
                        y = j + fy[k] * h;
                        if (x < 0 || x >= n || y < 0 || y >= n || a[x][y] != a[i][j]) break;
                        flag++;
                    }
                    if (flag == 5) {
                        if (a[i][j] == 'B') tb = 1;
                        if (a[i][j] == 'W') tw = 1;
                    }
                }
            }
        }
        if (tb && tw) printf("Both");
        else if (tb && !tw) printf("Black");
        else if (!tb && tw) printf("While");
        else printf("None");
        printf("\n");
    }
    system("pause");
    return 0;
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容