井字游戏

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.

Examples
input
Copy
X0X
.0.
.X.
output
second

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

输出

打印六个判决:firstsecondillegalthe first player wonthe second player won draw.

样例

输入
复制
X0X
.0.
.X.
输出
second
#include<iostream>
#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值