# CodeForces 3C Tic-tac-toe 井字棋盘游戏

C. Tic-tac-toe
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

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 × 3grid (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.
Input

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).

Output

Print one of the six verdicts: firstsecondillegalthe first player wonthe second player won or draw.

Sample test(s)
input
X0X
.0.
.X.

output
second

题目大意是在3*3格子里。X先下，0后下，.代表没下棋的格。连成3个就赢了。但是有好多不合法的。
没下满时，下一个给X下就输出 first；下一个给0下就输出 second；X赢输出  the first player won；
0赢就输出  the second player won；平局 draw；不合法 illegal。
其实代码真的简单写，但是要注意不合法的情况，有点坑。
1，棋盘上不能出现X和0同时连成3个；
2，X连成3个时，0不能出现和X一样个数的棋子；
3，0连成3个时，X不能出现比0多一个棋子的情况。
注意这些就很简单。
代码如下：（大一学生，代码比较烂，很复杂）
#include <iostream>
#include <math.h>
#include <stdlib.h>

using namespace std;

int main()
{
char a[3][3];
for(int i=0; i<3; i++)
{
cin>>a[i];
}
int sum[2]={0};
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
if(a[i][j]=='X')
sum[0]++;
else if(a[i][j]=='0')
sum[1]++;
}
}
if(sum[0]-sum[1]>1||sum[0]-sum[1]<0)
{
cout<<"illegal"<<endl;
return 0;
}
int f[2]= {0};
for(int i=0; i<3; i++)
{
if(a[i][0]=='X'&&a[i][0]==a[i][1]&&a[i][1]==a[i][2])
f[0]=1;
if(a[i][0]=='0'&&a[i][0]==a[i][1]&&a[i][1]==a[i][2])
f[1]=1;
}
for(int i=0; i<3; i++)
{
if(a[0][i]=='X'&&a[0][i]==a[1][i]&&a[1][i]==a[2][i])
f[0]=1;
if(a[0][i]=='0'&&a[0][i]==a[1][i]&&a[1][i]==a[2][i])
f[1]=1;
}
if(a[0][2]=='X'&&a[0][2]==a[1][1]&&a[1][1]==a[2][0])
f[0]=1;
if(a[0][2]=='0'&&a[0][2]==a[1][1]&&a[1][1]==a[2][0])
f[1]=1;
if(a[2][2]=='X'&&a[2][2]==a[1][1]&&a[1][1]==a[0][0])
f[0]=1;
if(a[2][2]=='0'&&a[2][2]==a[1][1]&&a[1][1]==a[0][0])
f[1]=1;
if(f[0]==1&&f[1]==1)
{
cout<<"illegal"<<endl;
return 0;
}
else if(f[0]==1&&f[1]!=1&&sum[0]==sum[1])
{
cout<<"illegal"<<endl;
return 0;
}
else if(f[0]==1&&f[1]!=1&&sum[0]-sum[1]==1)
{
cout<<"the first player won"<<endl;
return 0;
}
else if(f[1]==1&&f[0]!=1&&sum[0]-sum[1]==1)
{
cout<<"illegal"<<endl;
return 0;
}
else if(f[1]==1&&f[0]!=1&&sum[0]-sum[1]!=1)
{
cout<<"the second player won"<<endl;
return 0;
}
int num=sum[0]+sum[1];
if(num==9)
{
cout<<"draw"<<endl;
return 0;
}
else if(sum[0]==sum[1])
{
cout<<"first"<<endl;
return 0;
}
else if(sum[0]-sum[1]==1)
{
cout<<"second"<<endl;
return 0;
}
return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：CodeForces 3C Tic-tac-toe 井字棋盘游戏 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)