codeforces 3C 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: firstsecondillegalthe first player wonthe second player won or draw.


题目大意是给出一个tic-tac-toe的棋盘,先手的人是X,后手的人是0,判断棋盘是以下哪种情况:

1.先手的人获胜

2.后手的人获胜

3.平局

4.轮到先手的人走了

5.轮到后手的人走了

6.棋盘非法

这题就纯暴力吧,主要是判断棋盘非法比较坑,得仔细考虑一下。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int xcnt;
int ocnt;
char brd[3][3];

bool win(char sym) {
    for (int i = 0; i < 3; i++) {
        if (brd[i][0] == sym && brd[i][1] == sym && brd[i][2] == sym) return true;
        if (brd[0][i] == sym && brd[1][i] == sym && brd[2][i] == sym) return true;
    }

    if (brd[1][1] != sym) return false;

    if (brd[0][0] == sym && brd[2][2] == sym) return true;
    if (brd[0][2] == sym && brd[2][0] == sym) return true;

    return false;
}

bool legal(void) {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            xcnt += brd[i][j] == 'X';
            ocnt += brd[i][j] == '0';
        }
    }

    if (xcnt - ocnt > 1) return false;
    if (xcnt - ocnt < 0) return false;
    if (win('X') && xcnt == ocnt) return false;
    if (win('0') && xcnt - ocnt == 1) return false;

    return true;
}

int main() {
    xcnt = 0;
    ocnt = 0;
    
    char stmp[4];
    for (int i = 0; i < 3; i++) {
        scanf("%s", stmp);
        for (int j = 0; j < 3; j++) brd[i][j] = stmp[j];
    }

    do {
        if (!legal()) {
            printf("illegal\n");
            break;
        }
        if (win('X')) {
            printf("the first player won\n");
            break;
        }
        if (win('0')) {
            printf("the second player won\n");
            break;
        }
        if (xcnt + ocnt == 9) {
            printf("draw\n");
            break;
        }
        if (xcnt == ocnt) {
            printf("first\n");
            break;
        }
        if (xcnt - ocnt == 1) {
            printf("second\n");
            break;
        }
    } while (true);
    return 0;
}


你可能感兴趣的:(暴力)