练习cf1846BB. Rudolph and Tic-Tac-Toe

题目如下
B. Rudolph and Tic-Tac-Toe
time limit per test1 second
memory limit per test256 megabytes
Rudolph invented the game of tic-tac-toe for three players. It has classic rules, except for the third player who plays with pluses. Rudolf has a 3×3 field — the result of the completed game. Each field cell contains either a cross, or a nought, or a plus sign, or nothing. The game is won by the player who makes a horizontal, vertical or diagonal row of 3's of their symbols.

Rudolph wants to find the result of the game. Either exactly one of the three players won or it ended in a draw. It is guaranteed that multiple players cannot win at the same time.

Input
The first line contains one integer 𝑡 (1≤𝑡≤104) — the number of test cases.

Each test case consists of three lines, each of which consists of three characters. The symbol can be one of four: "X" means a cross, "O" means a nought, "+" means a plus, "." means an empty cell.

Output
For each test case, print the string "X" if the crosses won, "O" if the noughts won, "+" if the pluses won, "DRAW" if there was a draw.

题目大意
有两名玩家正在下井字棋,分别用“X”和“O”代表,“."代表空格,判断谁胜出,无人胜出则输出“DRAW“

题目分析
井字棋判断胜出的方法是谁的棋在横竖对角线任一连城一线(三格)即胜出,
选择按照行、列、对角线三种来遍历整个井字格,一旦发现连成三格即结束返回

点击查看代码
// 按照行检查
char row(char board[3][3]){
    for (int i = 0; i < 3; i++){
        char ch = board[i][0];
        if (ch != '.' && ch == board[i][1] && ch == board[i][2]){
            return ch;
        }
    }
    return '.';
}

// 按照列检查
char col(char board[3][3]) {
    for (int i = 0; i < 3; i++) {
        char ch = board[0][i];
        if (ch != '.' && ch == board[1][i] && ch == board[2][i]) {
            return ch;
        }
    }
    return '.';
}

// 检查对角线
char diag(char board[3][3]){
    char ch = board[1][1];
    if(ch != '.'){
        if (ch == board[0][0] && ch == board[2][2]) return ch;
        if (ch == board[0][2] && ch == board[2][0]) return ch;
    }
    return '.';
}

完整代码

点击查看代码
#include <stdio.h>

// 按照行检查
char row(char board[3][3]){
    for (int i = 0; i < 3; i++){
        char ch = board[i][0];
        if (ch != '.' && ch == board[i][1] && ch == board[i][2]){
            return ch;
        }
    }
    return '.';
}
// 按照列检查
char col(char board[3][3]) {
    for (int i = 0; i < 3; i++) {
        char ch = board[0][i];
        if (ch != '.' && ch == board[1][i] && ch == board[2][i]) {
            return ch;
        }
    }
    return '.';
}
// 检查对角线
char diag(char board[3][3]){
    char ch = board[1][1];
    if(ch != '.'){
        if (ch == board[0][0] && ch == board[2][2]) return ch;
        if (ch == board[0][2] && ch == board[2][0]) return ch;
    }
    return '.';
}
int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        char field[3][3];
        for (int i = 0; i < 3; i++){
            for (int j = 0; j < 3; j++){
                scanf(" %c", &field[i][j]);
            }
        }
        char r = row(field);
        if(r != '.'){
            printf("%c\n", r);
            continue;
        }
        char c = col(field);
        if(c != '.'){
            printf("%c\n", c);
            continue;
        }
        char d = diag(field);
        if (d != '.'){
            printf("%c\n", d);
            continue;
        }
        printf("DRAW\n");
    }
    return 0;
}
posted @ 2025-07-11 20:39  sirro1uta  阅读(15)  评论(0)    收藏  举报