/*
Name: 原创五子连珠
Author: 巧若拙
Description:
描述
五子棋中出现同一行,列,斜线的连续5个棋子颜色相同时,该色棋子胜出。
现在给定一个9*9大小的棋盘局面,请你判断哪方胜出。
输入
9行,每行有9个用空格隔开的数字。其中1表示黑子,2表示白子,0表示空白
输出
1个字符串,"黑胜","白胜"或"无胜负",输入确保只可能出现一种情况
样例输入1
0 0 0 0 2 0 0 0 0
0 0 0 0 1 0 0 0 2
0 0 0 0 1 0 0 2 0
0 0 0 0 1 0 2 0 0
0 0 0 0 1 2 0 0 0
0 0 0 1 2 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
样例输入2
0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 2 0
0 0 0 0 1 0 2 0 0
0 0 0 0 1 2 0 0 0
0 0 0 0 2 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
样例输入3
0 0 0 0 0 0 0 2 0
0 0 0 0 0 0 1 0 0
0 0 1 0 0 1 0 0 0
0 0 0 2 1 1 0 0 0
0 0 0 1 2 0 0 0 0
0 0 2 0 0 2 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
样例输出1
白胜
样例输出2
黑胜
样例输出3
无胜负
*/
#include <iostream>
using namespace std;
const int N = 9;
int A[N][N];
int main()
{
int n = 9;
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
cin >> A[i][j];
}
}
int win = 0;
for (int i=0; i<n && win==0; i++)
{
for (int j=0; j<n && win==0; j++)
{
if (A[i][j] == 0) //跳过空白交叉点
continue;
if (j+4<n && A[i][j]==A[i][j+1] && A[i][j]==A[i][j+2] && A[i][j]==A[i][j+3] && A[i][j]==A[i][j+4])//向右搜索
win = A[i][j];
if (i+4<n && A[i][j]==A[i+1][j] && A[i][j]==A[i+2][j] && A[i][j]==A[i+3][j] && A[i][j]==A[i+4][j])//向下搜索
win = A[i][j];
if (i+4<n && j+4<n && A[i][j]==A[i+1][j+1] && A[i][j]==A[i+2][j+2] && A[i][j]==A[i+3][j+3] && A[i][j]==A[i+4][j+4])//向右下搜索
win = A[i][j];
if (i+4<n && j-4>=0 && A[i][j]==A[i+1][j-1] && A[i][j]==A[i+2][j-2] && A[i][j]==A[i+3][j-3] && A[i][j]==A[i+4][j-4])//向左下搜索
win = A[i][j];
}
}
if (win == 0)
cout << "无胜负" << endl;
else if (win == 1)
cout << "黑胜" << endl;
else
cout << "白胜" << endl;
return 0;
}
二维数组练习原创五子连珠
最新推荐文章于 2023-11-05 13:37:43 发布