题解:UVA10363 Tic Tac Toe

· · 题解

题解:UVA10363 Tic Tac Toe

题外话:我真没账号,求放过。

题意

现在有一个棋盘,只包含 X(先手)、O(后手)、.(空)。

现在问你这种棋局是否可能存在(不是问你赢没赢)。

注意:N 组数据。

思路

不会真有人只会刷题而不会玩井字棋吧。

只要当前棋局满足以下两个条件合法:

代码

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
int N;
int main()
{
    cin >> N;
    while (N--)
    {
        // 多测记得清空
        char x;
        int t[5][5], X = 0, O = 0;
        for (int i = 1; i <= 9; i++)
        {
            cin >> x;
            if (x == '.')
                t[i / 3][i % 3] = 0;
            if (x == 'X')
            {
                t[i / 3][i % 3] = 1;
                X++;
            }
            if (x == 'O')
            {
                t[i / 3][i % 3] = -1;
                O++;
            }
        }
        if (O + 1 == X)
        {
            if (t[0][0] + t[0][1] + t[0][2] != -3 &&
                t[1][0] + t[1][1] + t[1][2] != -3 &&
                t[2][0] + t[2][1] + t[2][2] != -3 &&
                t[1][0] + t[2][0] + t[3][0] != -3 &&
                t[0][0] + t[1][0] + t[2][0] != -3 &&
                t[0][1] + t[1][1] + t[2][1] != -3 &&
                t[0][2] + t[1][2] + t[2][2] != -3 &&
                t[0][0] + t[1][1] + t[2][2] != -3 &&
                t[0][2] + t[1][1] + t[2][0] != -3)
                cout << "yes" << endl;
            else
                cout << "no" << endl;
        }
        else if (X == O)
        {
            if (t[0][0] + t[0][1] + t[0][2] != 3 &&
                t[1][0] + t[1][1] + t[1][2] != 3 &&
                t[2][0] + t[2][1] + t[2][2] != 3 &&
                t[1][0] + t[2][0] + t[3][0] != 3 &&
                t[0][0] + t[1][0] + t[2][0] != 3 &&
                t[0][1] + t[1][1] + t[2][1] != 3 &&
                t[0][2] + t[1][2] + t[2][2] != 3 &&
                t[0][0] + t[1][1] + t[2][2] != 3 &&
                t[0][2] + t[1][1] + t[2][0] != 3)
                cout << "yes" << endl;
            else
                cout << "no" << endl;
        }
        else
            cout << "no" << endl;
    }
    return 0;
}