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

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

翻译:

 时间限制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
# include
# include
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 {
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"< return 0;
}
}

你可能感兴趣的:(C. Tic-tac-toe(井字棋游戏))