每日一题之 hiho 204 Five in a Row

本文介绍了一种用于判定五子棋胜负的算法实现。通过遍历棋盘上的每个位置,检查横向、纵向及两个对角线方向上是否存在连续五个相同颜色的棋子,从而确定胜负结果。

题目1 : Five in a Row
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
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

题意:

一个五子棋的棋盘,判断棋盘中五子连珠的情况,包括横向纵向和对角线满足有五颗连续的棋子算胜利。

思路:

遍历棋盘的每个位置,那么从当前位置来看,可以进行连珠的方向有4种情况分别是 左下,下,右下, 右,然后直接暴力找就行了。

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;


int dir[4][2] = {{1,1},{1,0},{0,1},{-1,1}};

bool is_ok(int i, int j,int k)
{
    if (i + dir[k][0] >= 0 && i + dir[k][0] < 15 && j + dir[k][1] >= 0 && j + dir[k][1] < 15)
        return true;
    return false;
}

int main()
{
    int t;
    cin >> t;
    vector<string>S;
    string str;
    while(t--){

        S.clear();  
        for (int i = 0; i < 15; ++i) {
            cin>>str;
            S.push_back(str);
        }
        int w_i = 0,b_i = 0;
        int w_j = 0,b_j = 0;
        int W[3],B[3];

        bool res_w = false;
        bool res_b = false;

        for (int i = 0; i < 15; ++i) {
            for (int j = 0; j < 15;++j) {
                if (S[i][j] == 'W') {

                    for (int k = 0; k < 4; ++k) {
                        w_i = i;
                        w_j = j;
                        W[k] = 1;

                        while(is_ok(w_i,w_j,k) && S[w_i + dir[k][0]][w_j + dir[k][1]] == 'W') {
                            w_i += dir[k][0];
                            w_j += dir[k][1];
                            ++W[k];
                        }
                        if (W[k] >= 5) res_w = true;
                    }
                }
                else if (S[i][j] == 'B') {

                    for (int k = 0; k < 4; ++k) {
                        w_i = i;
                        w_j = j;
                        B[k] = 1;

                        while(is_ok(w_i,w_j,k) && S[w_i + dir[k][0]][w_j + dir[k][1]] == 'B') {
                            w_i += dir[k][0];
                            w_j += dir[k][1];
                            ++B[k];
                        }
                        if (B[k] >= 5) res_b = true;
                    }

                }
            }
        }
    if (res_w && res_b) cout << "Both" << endl;
    else if (res_w && !res_b) cout << "White" << endl;
    else if (!res_w && res_b)cout << "Black" << endl;
    else cout << "None" << endl;
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值