【题目描述】
Certainly, everyone is familiar with tic-tac-toe game. The rules are very simple indeed. Two players take turns marking the cells in a 3 × 3 grid (one player always draws crosses, the other — noughts). The player who succeeds first in placing three of his marks in a horizontal, vertical or diagonal line wins, and the game is finished. The player who draws crosses goes first. If the grid is filled, but neither Xs, nor 0s form the required line, a draw is announced.
You are given a 3 × 3 grid, each grid cell is empty, or occupied by a cross or a nought. You have to find the player (first or second), whose turn is next, or print one of the verdicts below:
illegal — if the given board layout can’t appear during a valid game;
the first player won — if in the given board layout the first player has just won;
the second player won — if in the given board layout the second player has just won;
draw — if the given board layout has just let to a draw.
【输入】
The input consists of three lines, each of the lines contains characters “.”, “X” or “0” (a period, a capital letter X, or a digit zero).
【输出】
Print one of the six verdicts: first, second, illegal, the first player won, the second player won or draw.
【样例输入】
X0X
.0.
.X.
【样例输出】
second
题目链接:https://codeforces.com/contest/3/problem/C
这怕是最水的C题了吧
代码如下:
#include <bits/stdc++.h>
using namespace std;
char a[4][4];
bool check(char c)
{
for(int i=1;i<=3;i++)
if(a[i][1]==c && a[i][2]==c && a[i][3]==c)
return true;
for(int i=1;i<=3;i++)
if(a[1][i]==c && a[2][i]==c && a[3][i]==c)
return true;
if(a[1][1]==c && a[2][2]==c && a[3][3]==c)
return true;
if(a[1][3]==c && a[2][2]==c && a[3][1]==c)
return true;
return false;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0),cout.tie(0);
int X=0,O=0;
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
{
cin>>a[i][j];
if(a[i][j]=='X')
X++;
else if(a[i][j]=='0')
O++;
}
bool f1=check('X'),f2=check('0');
if((f1 && f2)||(f1 && X-O!=1)||(f2 && X!=O)||(X-O>1 || X-O<0))
cout<<"illegal"<<endl;
else if(f1)
cout<<"the first player won"<<endl;
else if(f2)
cout<<"the second player won"<<endl;
else if(X+O==9)
cout<<"draw"<<endl;
else if(X==O)
cout<<"first"<<endl;
else if(X-O==1)
cout<<"second"<<endl;
return 0;
}