Codeforces Beta Round #3 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: 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;
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法