11261: 【原1261】LittleGame
题目
题目描述
author: yixi 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/1261
Description
小卡和欢总喜欢玩一个小游戏,游戏界面是4*4格子的棋盘,每个格子可以放一个棋子。
最开始的时候棋盘上只有一个棋子,叫'T'。
接下来欢总和小卡轮流放棋子,小卡的棋子是'X',欢总是'O'。
如果有一行或一列或一条对角线上全部是某个选手的棋子(4个)或者3个他的棋子和'T',这个选手就赢了,游戏结束。 如果所有格子都满了,没有人赢,以平局结束。
给你一个棋局,你要告诉我们:
"X won" 游戏结束,小卡赢了;
"O won" 游戏结束,欢总赢了;
"Draw" 游戏结束,平局结束;
"Game has not completed" 游戏还没结束。
如果还有空格子,那么游戏还没结束,你应该输出"Game has not completed"。
Input Format
第一行给出数据组数T,1<=T<=500。
接下来T组,每组4行,每行4个字符,是'X','O','T'或'.'。其中'.'表示空格子。
每组结束之后有一个空行。
Output Format
输出T行,每行输出一个结果。
Sample Input
6
XXXT
....
OO..
....
XOXT
XXOO
OXOX
XXOO
XOX.
OX..
....
....
OOXX
OXXX
OX.T
O..O
XXXO
..O.
.O..
T...
OXXX
XO..
..O.
...O
Sample Output
X won
Draw
Game has not completed
O won
O won
O won
yyong119's solution
#include <iostream>
#include <cstring>
using namespace std;
int T;
int X, O;
int mapX[4][4], mapO[4][4];
int main() {
ios::sync_with_stdio(false);
cin >> T;
while(T--) {
memset(mapX, 0, sizeof(mapX));
memset(mapO, 0, sizeof(mapO));
int foundX = 0, foundO = 0, full = 1;
char tmp;
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j) {
cin >> tmp;
if (tmp == 'X' || tmp == 'T') mapX[i][j] = 1;
if (tmp == 'O' || tmp == 'T') mapO[i][j] = 1;
if (tmp == '.') full = 0;
}
// find X
for (int i = 0; i < 4; ++i) {
if (mapX[i][0] && mapX[i][1] && mapX[i][2] && mapX[i][3]) {
foundX = 1;
break;
}
if (mapX[0][i] && mapX[1][i] && mapX[2][i] && mapX[3][i]) {
foundX = 1;
break;
}
}
if (mapX[0][0] && mapX[1][1] && mapX[2][2] && mapX[3][3]) foundX = 1;
if (mapX[0][3] && mapX[1][2] && mapX[2][1] && mapX[3][0]) foundX = 1;
// find O
for (int i = 0; i < 4; ++i) {
if (mapO[i][0] && mapO[i][1] && mapO[i][2] && mapO[i][3]) {
foundO = 1;
break;
}
if (mapO[0][i] && mapO[1][i] && mapO[2][i] && mapO[3][i]) {
foundO = 1;
break;
}
}
if (mapO[0][0] && mapO[1][1] && mapO[2][2] && mapO[3][3]) foundO = 1;
if (mapO[0][3] && mapO[1][2] && mapO[2][1] && mapO[3][0]) foundO = 1;
// check
if (foundX) cout << "X won" << endl;
else if (foundO) cout << "O won" << endl;
else if (full && !foundX && ! foundO) cout << "Draw" << endl;
else cout << "Game has not completed" << endl;
}
}