关闭

CF 3C Tic-tac-toe

标签: codeforces
226人阅读 评论(0) 收藏 举报
分类:

此题只要考虑全面就可以额

愁死了

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

char data[5][5];
int _win[2][10];// 行123 列123 \/
int judgement(char c)
{
    int judge = 0;
    for(int i = 0; i < 3; i ++)
    {
        int flag = 1;
        for(int j = 0; j < 3; j ++)
        {
            if(data[i][j] != c)
                flag = 0;
        }
        if(flag)
        {
            judge = 1;
            if(c == 'X')
                _win[0][i] = 1;
            if(c == '0')
                _win[1][i] = 1;
        }
    }
    for(int i = 0; i < 3; i ++)
    {
        int flag = 1;
        for(int j = 0; j < 3; j ++)
        {
            if(data[j][i] != c)
                flag = 0;
        }
        if(flag)
        {
            judge = 1;
            if(c == 'X')
                _win[0][3+i] = 1;
            if(c == '0')
                _win[1][3+i] = 1;
        }
    }
    if(data[0][0] == data[1][1] && data[1][1] == data[2][2] && data[0][0] == c)//\/
    {
        judge = 1;
        if(c == 'X')
            _win[0][6] = 1;
        if(c == '0')
            _win[1][6] = 1;
    }
    if(data[0][2] == data[1][1] && data[1][1] == data[2][0] && data[0][2] == c)// /
    {
        judge = 1;
        if(c == 'X')
            _win[0][7] = 1;
        if(c == '0')
            _win[1][7] = 1;
    }
    return judge;
}
int main()
{
    while(cin>>data[0]>>data[1]>>data[2])
    {
        memset(_win,0,sizeof(_win));
        int sum1 = 0,sum2 = 0;
        for(int i = 0; i < 3; i ++)
        {
            for(int j = 0; j < 3; j ++)
            {
                if(data[i][j] == 'X')
                    sum1 ++;
                if(data[i][j] == '0')
                    sum2 ++;
            }
        }
        int flag1 = 0,flag2 = 0;
        flag1 = judgement('X');
        flag2 = judgement('0');
        if(flag1 && flag2)
        {
            cout<<"illegal"<<endl;
            continue;
        }
        else if(!(sum1==sum2)&&!(sum1-sum2 == 1))
        {
            cout<<"illegal"<<endl;
            continue ;
        }
        else if(!flag1 &&!flag2)
        {
            if(sum1 == 5 && sum2 == 4)
                cout<<"draw"<<endl;
            else if(sum1-sum2 ==1)
                cout<<"second"<<endl;
            else if(sum1 == sum2)
                cout<<"first"<<endl;
            continue;
        }
        else if(flag1 &&!flag2)
        {
            int flag = 0;
            for(int i = 0; i < 3; i ++)
            {
                for(int j = 3; j < 6; j ++)
                {
                    if(_win[0][j] == _win[0][i] &&_win[0][i] == 1)
                        flag = 1;
                }
                if(flag) break;
            }
            if(_win[0][6] == _win[0][7] &&_win[0][6]==1)
                flag = 1;
            if(flag)
            {
                cout<<"the first player won"<<endl;
                continue;
            }
            if(sum1 - sum2 == 1)
            {
                cout<<"the first player won"<<endl;
                continue ;
            }
            else
            {
                cout<<"illegal"<<endl;
                continue ;
            }
        }
        else if(!flag1 &&flag2)
        {
            if(sum2 == sum1)
            {
                cout<<"the second player won"<<endl;
                continue ;
            }
            else
            {
                cout<<"illegal"<<endl;
                continue ;
            }
        }
    }
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15546次
    • 积分:903
    • 等级:
    • 排名:千里之外
    • 原创:79篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条
    文章分类