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:
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
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#include<unordered_set>
#define rg register ll
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 200005
const double eps = 1e-6;
using namespace std;
inline ll read()
{
char ch = getchar(); ll s = 0, w = 1;
while (ch < 48 || ch>57) { if (ch == '-')w = -1; ch = getchar(); }
while (ch >= 48 && ch <= 57) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
return s * w;
}
inline void write(ll x)
{
if (x < 0)putchar('-'), x = -x;
if (x > 9)write(x / 10);
putchar(x % 10 + 48);
}
char s[5][5];
ll num1,num2;
int main()
{
for(rg i=1;i<=3;i++)
{
for(rg j=1;j<=3;j++)
{
cin>>s[i][j];
if(s[i][j]=='X')num1++;
if(s[i][j]=='0')num2++;
}
}
//cout<<num1<<num2<<endl;
if(num1<num2||num1>num2+1)
{
cout<<"illegal"<<endl;
return 0;
}
ll flag1=0,sum=0,flag2=0;
if(s[1][1]==s[1][2]&&s[1][2]==s[1][3]&&s[1][1]!='.')
{
if(s[1][1]=='X')
{
flag1=1,sum++;
}
else
{
flag2=1,sum++;
}
}
if(s[2][1]==s[2][2]&&s[2][2]==s[2][3]&&s[2][2]!='.')
{
if(s[2][2]=='X')
{
flag1=1,sum++;
}
else
{
flag2=1,sum++;
}
}
if(s[3][1]==s[3][2]&&s[3][2]==s[3][3]&&s[3][1]!='.')
{
if(s[3][1]=='X')
{
flag1=1,sum++;
}
else
{
flag2=1,sum++;
}
}
if(s[1][1]==s[2][1]&&s[1][1]==s[3][1]&&s[1][1]!='.')
{
if(s[1][1]=='X')
{
flag1=1,sum++;
}
else
{
flag2=1,sum++;
}
}
if(s[1][2]==s[2][2]&&s[2][2]==s[3][2]&&s[1][2]!='.')
{
if(s[1][2]=='X')
{
flag1=1,sum++;
}
else
{
flag2=1,sum++;
}
}
if(s[1][3]==s[2][3]&&s[1][3]==s[3][3]&&s[1][3]!='.')
{
if(s[1][3]=='X')
{
flag1=1,sum++;
}
else
{
flag2=1,sum++;
}
}
if(s[1][1]==s[2][2]&&s[1][1]==s[3][3]&&s[1][1]!='.')
{
if(s[1][1]=='X')
{
flag1=1,sum++;
}
else
{
flag2=1,sum++;
}
}
if(s[1][3]==s[2][2]&&s[1][3]==s[3][1]&&s[1][3]!='.')
{
if(s[1][3]=='X')
{
flag1=1,sum++;
}
else
{
flag2=1,sum++;
}
}
if(flag1&&num1-num2==1)
{
cout<<"the first player won"<<endl;
return 0;
}
else if(flag1&&num1-num2!=1)
{
cout<<"illegal"<<endl;
return 0;
}
if(flag2&&num1==num2)
{
cout<<"the second player won"<<endl;
return 0;
}
else if(flag2&&num1!=num2)
{
cout<<"illegal"<<endl;
return 0;
}
if(num1+num2<9)
{
if(num1>num2)
{
cout<<"second"<<endl;
return 0;
}
else cout<<"first"<<endl;
}
else
{
cout<<"draw"<<endl;
return 0;
}
return 0;
}