前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces Beta Round #3 C. Tic-tac-toe

Codeforces Beta Round #3 C. Tic-tac-toe

作者头像
glm233
发布2020-09-28 10:44:23
5340
发布2020-09-28 10:44:23
举报
文章被收录于专栏:glm的全栈学习之路

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

代码语言:javascript
复制
X0X
.0.
.X.

output

Copy

代码语言:javascript
复制
second
代码语言:javascript
复制
// 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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档