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

440

被折叠的 条评论
为什么被折叠?



