翻译:
时间限制1秒钟;
内存限制64mb;
标准输入输出;
可以肯定,每个人都对三连棋(井字棋)非常熟悉。它的规则的确非常简单。两个玩家轮流在3×3网格棋盘标记(一个玩家总是画×,另一个画0)那个玩家的三个标记先都在水平线上、垂直线上或对角线上连成一条线那个玩家就获胜了,然后游戏就结束了。如果棋盘满了,但画×的还是画0的都没赢,那一局就平局了。
首先你要输入一个3×3的棋局,每个单元网格都是空的,或有些X或0占用,你必须找出轮到那个玩家(第一或第二)下了,或输出下面的判决之一。
- illegal(非法的) —— 如果给定的棋盘不会在游戏中出现。
- the first player won(玩家一获胜) —— 给定的棋局刚好玩家一获胜。
- the second player won(玩家二获胜) —— 给定的棋局刚好玩家二获胜。
- draw(平局) —— 如果给定的棋局刚好是一个平局。
输入
由三行组成,每行包括“.”“X”“0”(每个格只能有一个大写字母“X”,或有一个数字0)。
输出
输出六个判决中的一个: first, second, illegal, the first player won, the second player won 或 draw。
样例
X0X .0. .X.
second
c++程序:
# include<iostream>
# include<cstdio>
# include<cstring>
using namespace std;
int a,b,c,d,p1=0,p2=0;
char cp[4][4];
int main()
{
for(a=1;a<=3;a++)
for(b=1;b<=3;b++)
{
cin>>cp[a][b];
if(cp[a][b]=='X')
p1++;
else if(cp[a][b]=='0')
p2++;
}
if(p1-p2>1||p1<p2)
{
printf("illegal\n");
return 0;
}
else if(p1==p2&&p1!=5)
{
printf("first\n");
return 0;
}
else if(p1-p2==1&&p2!=4)
{
printf("second\n");
return 0;
}
else if(cp[2][2]=='X'&&cp[1][2]=='X'&&cp[3][2]=='X'||cp[2][2]=='X'&&cp[2][1]=='X'&&cp[2][3]=='X'||cp[2][2]=='X'&&cp[1][1]=='X'&&cp[3][3]=='X'||cp[2][2]=='X'&&cp[1][3]=='X'&&cp[3][1]=='X'||cp[1][1]=='X'&&cp[1][2]=='X'&&cp[1][3]=='X'||cp[3][1]=='X'&&cp[3][2]=='X'&&cp[3][3]=='X'||cp[1][1]=='X'&&cp[2][1]=='X'&&cp[3][1]=='X'||cp[1][3]=='X'&&cp[2][3]=='X'&&cp[3][3]=='X')
{
printf("the first player won\n");
return 0;
}
else if(cp[2][2]=='0'&&cp[1][2]=='0'&&cp[3][2]=='0'||cp[2][2]=='0'&&cp[2][1]=='0'&&cp[2][3]=='0'||cp[2][2]=='0'&&cp[1][1]=='0'&&cp[3][3]=='0'||cp[2][2]=='0'&&cp[1][3]=='0'&&cp[3][1]=='0'||cp[1][1]=='0'&&cp[1][2]=='0'&&cp[1][3]=='0'||cp[3][1]=='0'&&cp[3][2]=='0'&&cp[3][3]=='0'||cp[1][1]=='0'&&cp[2][1]=='0'&&cp[3][1]=='0'||cp[1][3]=='0'&&cp[2][3]=='0'&&cp[3][3]=='0')
{
printf("the second player won\n");
return 0;
}
else
{
cout<<"draw"<<endl;
return 0;
}
}