跳到正文

题解

题解:P17093 [ICPC 2017 Qingdao R] Chenchen, Tangtang and ZengZeng

zhedaotixuanbo约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年7月12日
收录
2026年7月14日
更新
2026年11月16日
篇幅
约 3 分钟
获赞
1
题目
P17093

版本

  1. v1当前

题面传送门:P17093 [ICPC 2017 Qingdao R] Chenchen, Tangtang and ZengZeng

题目大意

给定多个三子棋棋盘,判断获胜情况。

思路讲解

本题可用字符数组存储。由于只有三行三列,我们可以枚举数组中每行每列每对角线是否有三个相同棋子(但要注意不是空白)。若有则有人获胜,输出这三格的值即可。在枚举完还没有输出结果时,输出一个 ongoing 即可。

对于如何判断有无输出,既可以使用标记变量,有输出就打标记;也可使用 continue 语句,若有输出直接不执行下方的输出 ongoing 指令。

代码实现

完整代码
C++
#include<bits/stdc++.h>
using namespace std;
int t;
char c[4][4];
int main(){
	cin>>t;
	for(int z=1;z<=t;z++){
		for(int i=1;i<=3;i++){
			for(int j=1;j<=3;j++){
				cin>>c[i][j];
			}
		}
		if(c[1][1]==c[1][2]&&c[1][2]==c[1][3]&&c[1][1]!='.'){
			cout<<c[1][1]<<'\n';
			continue;
		}
		if(c[2][1]==c[2][2]&&c[2][2]==c[2][3]&&c[2][1]!='.'){
			cout<<c[2][1]<<'\n';
			continue;
		}
		if(c[3][1]==c[3][2]&&c[3][2]==c[3][3]&&c[3][1]!='.'){
			cout<<c[3][1]<<'\n';
			continue;
		}
		if(c[1][1]==c[2][1]&&c[2][1]==c[3][1]&&c[1][1]!='.'){
			cout<<c[1][1]<<'\n';
			continue;
		}
		if(c[1][2]==c[2][2]&&c[2][2]==c[3][2]&&c[1][2]!='.'){
			cout<<c[1][2]<<'\n';
			continue;
		}
		if(c[1][3]==c[2][3]&&c[2][3]==c[3][3]&&c[1][3]!='.'){
			cout<<c[1][3]<<'\n';
			continue;
		}
		if(c[1][1]==c[2][2]&&c[2][2]==c[3][3]&&c[1][1]!='.'){
			cout<<c[1][1]<<'\n';
			continue;
		}
		if(c[3][1]==c[2][2]&&c[2][2]==c[1][3]&&c[3][1]!='.'){
			cout<<c[3][1]<<'\n';
			continue;
		}
		cout<<"ongoing"<<'\n';
	}
	return 0;
}