C.井字游戏
时间限制:1秒
内存限制:64mb
输入:常规输入
输出:常规输出
当然,每个人都很熟悉井字游戏。他的规则确实很简单。两个玩家在一个3*3的方格中标记记号(总是一个玩家画X,另一个玩家画0)。哪个玩家先把他的三个记号连成一条线他就成功了,垂直线或斜线都可以赢,并且这个游戏就结束了。画X的玩家先画,如果方格用完了,但是画X的玩家和画O的玩家都没有画出所需的线,那就宣布是平局。
你有一个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
#include<algorithm>
using namespace std;
const int n=3;
char q[n][n];
int js(char c)
{
int i,j,ans=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
ans += q[i][j]==c;
return ans;
}
bool w(char c)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(q[i][j]!=c)
break;
if(j==n)
return true;
}
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
if(q[i][j]!=c)
break;
if(i==n)
return true;
}
for(i=0;i<n;i++)
if(q[i][i]!=c)
break;
if(i==n)
return true;
for(i=0;i<n;i++)
if(q[i][n-1-i]!=c)
break;
return i==n;
}
int main()
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>q[i][j];
int jsx=js('X'),jso=js('0');
bool wx=w('X'),wo=w('0');
if(jsx<jso||jsx>jso+1||wx&&jsx==jso||wo&&jsx==jso+1||wx&&wo)
cout << "illegal";
else if(wx)
cout << "the first player won";
else if(wo)
cout << "the second player won";
else if(jsx+jso==n*n)
cout << "draw";
else if(jsx==jso)
cout << "first";
else if(jsx==jso+1)
cout << "second";
else
cout << "illegal";
return 0;
}