链接:https://codeforces.com/problemset/problem/3/C
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: first, second, illegal, the first player won, the second player won or draw.
Examples
input
Copy
X0X .0. .X.
output
Copy
second
代码:
#include<bits/stdc++.h>
using namespace std;
long long n,t,a,b,c,l,j,r,k,d,ans,max1=0,mod=1e9+7;
char x[10][10];
long long m1,m2;
int main()
{
m1=0;
m2=0;
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
cin>>x[i][j];
if(x[i][j]=='0')
m1++;
else if(x[i][j]=='X')
m2++;
}
}
k=0;
if(m1>m2||m2-m1>1)
cout<<"illegal";
else
{
if(x[1][1]==x[1][2]&&x[1][1]==x[1][3]||(x[1][1]==x[2][2]&&x[1][1]==x[3][3])||(x[1][1]==x[2][1]&&x[1][1]==x[3][1]))
{
if(x[1][1]=='X'&&k==0)
k=1;
else if(x[1][1]=='X'&&k==2)
{
cout<<"illegal";
return 0;
}
else if(x[1][1]=='0'&&k==0)
{
k=2;
}
else if(x[1][1]=='0'&&k==1)
{
cout<<"illegal";
return 0;
}
}
if(x[2][1]==x[2][2]&&x[2][2]==x[2][3])
{
if(x[2][1]=='X'&&k==0)
k=1;
else if(x[2][1]=='X'&&k==2)
{
cout<<"illegal";
return 0;
}
else if(x[2][1]=='0'&&k==0)
{
k=2;
}
else if(x[2][1]=='0'&&k==1)
{
cout<<"illegal";
return 0;
}
}
if(x[3][1]==x[3][2]&&x[3][2]==x[3][3]||(x[3][1]==x[2][2]&&x[2][2]==x[1][3]))
{
if(x[3][1]=='X'&&k==0)
k=1;
else if(x[3][1]=='X'&&k==2)
{
cout<<"illegal";
return 0;
}
else if(x[3][1]=='0'&&k==0)
{
k=2;
}
else if(x[3][1]=='0'&&k==1)
{
cout<<"illegal";
return 0;
}
}
if(x[1][2]==x[2][2]&&x[2][2]==x[3][2])
{
if(x[1][2]=='X'&&k==0)
k=1;
else if(x[1][2]=='X'&&k==2)
{
cout<<"illegal";
return 0;
}
else if(x[1][2]=='0'&&k==0)
{
k=2;
}
else if(x[1][2]=='0'&&k==1)
{
cout<<"illegal";
return 0;
}
}
if(x[1][3]==x[2][3]&&x[2][3]==x[3][3])
{
if(x[1][3]=='X'&&k==0)
k=1;
else if(x[1][3]=='X'&&k==2)
{
cout<<"illegal";
return 0;
}
else if(x[1][3]=='0'&&k==0)
{
k=2;
}
else if(x[1][3]=='0'&&k==1)
{
cout<<"illegal";
return 0;
}
}
if(k==1&&m1==m2-1)
cout<<"the first player won";
else if(k==1)
cout<<"illegal";
else if(k==2&&m1==m2)
{
cout<<"the second player won";
}
else if(k==2)
{
cout<<"illegal";
}
else
{
if(m1+m2==9)
cout<<"draw";
else
{
if(m1<m2)
cout<<"second";
else
cout<<"first";
}
}
}
}

本文介绍了一种用于判断井字游戏(Tic-Tac-Toe)胜负的算法,通过分析棋盘状态来确定游戏结果,包括玩家胜利、平局或非法布局。代码实现详细展示了如何检查水平、垂直和对角线上的连续标记,以判断是否有玩家赢得比赛。
3242

被折叠的 条评论
为什么被折叠?



