Codeforces3C. Tic-tac-toe 题解 状态压缩+搜索

Posted codedecision

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces3C. Tic-tac-toe 题解 状态压缩+搜索相关的知识,希望对你有一定的参考价值。

题目链接:https://codeforces.com/contest/3/problem/C
题目大意:
有一个 (3 imes 3) 的棋盘,给你一个棋盘当前的状态,请你输出当前这个状态对应的描述。
解题思路:
(3 imes 3) 的棋盘上一共有9个位置,每个位置只有可能是3种状态:“.”、“0”或“X”。
所以总的状态数有 (3^9) 种,我们可以从初始状态开始来搜索遍历得到所有的状态对应的描述(如果一个状态通过搜索遍历不到,那么这个状态就是 illegal 的)。

代码解释

get_status_num():获得当前状态对应的状态码。
status与状态对应关系:
illegal -- 0
first -- 1
second -- 2
the first player won -- 3
the second player won -- 4
draw -- 5

check_who_win():判断当前状态谁赢

  • 返回1: the first win
  • 返回2:the second win
  • 返回0:还没有人赢

check_whos_turn():判断当前是应该谁下棋

  • 返回1:first
  • 返回2:second

最后我们从初始状态搜索一下,就能够得到所有的状态对应的描述。

实现代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 20000; // 棋盘总共有3^9=19683种状态
char grid[3][4];
int status[maxn];
/**
status与状态对应关系
illegal -- 0
first   -- 1
second  -- 2
the first player won    -- 3
the second player won   -- 4
draw    -- 5
*/
/*
 * get_status_num()获得当前状态对应的状态码
*/
int get_status_num() {
    int t = 1, ans = 1;
    for (int i = 0; i < 3; i ++) {
        for (int j = 0; j < 3; j ++) {
            int a;
            switch (grid[i][j]) {
                case 'X': a = 1; break;
                case '0': a = 2; break;
                default: a = 0;
            }
            ans += a * t;
            t *= 3;
        }
    }
    return ans;
}
/*
 * check_who_win判断当前状态谁赢
 * 返回1: the first win
 * 返回2:the second win
 * 返回0:还没有人赢
*/
int check_who_win() {
    for (int i = 0; i < 3; i ++) {
        if (grid[i][0]!='.' && grid[i][0]==grid[i][1] && grid[i][0]==grid[i][2]) {
            return (grid[i][0] == 'X') ? 1 : 2;
        }
        if (grid[0][i]!='.' && grid[0][i]==grid[1][i] && grid[0][i]==grid[2][i]) {
            return (grid[0][i] == 'X') ? 1 : 2;
        }
    }
    for (int i = -1; i <= 1; i += 2) {
        if (grid[1][1]!='.' && grid[1][1]==grid[0][1-i] && grid[1][1]==grid[2][1+i]) {
            return (grid[1][1] == 'X') ? 1 : 2;
        }
    }
    return 0;
}
/*
 * check_whos_turn判断当前是应该谁下棋
 * 返回1:first
 * 返回2:second
*/
int check_whos_turn() {
    int cnt1 = 0, cnt2 = 0;
    for (int i = 0; i < 3; i ++) for (int j = 0; j < 3; j ++) if (grid[i][j] != '.') {
        if (grid[i][j] == 'X') cnt1 ++;
        else cnt2 ++;
    }
    if (cnt1 == cnt2) return 1;
    else return 2;
}

void init() {
    for (int i = 0; i < 3; i ++) for (int j = 0; j < 3; j ++) grid[i][j] = '.';
}

void dfs() {
    int status_num = get_status_num();
    int winner_id = check_who_win();
    if (winner_id) {
        status[status_num] = winner_id + 2;
    }
    else {
        int turn_id = check_whos_turn();
        status[status_num] = turn_id;
        char chess = (turn_id == 1) ? 'X' : '0';
        bool has_place_to_put = false;
        for (int i = 0; i < 3; i ++) {
            for (int j = 0; j < 3; j ++) {
                if (grid[i][j] == '.') {
                    has_place_to_put = true;
                    grid[i][j] = chess;
                    dfs();
                    grid[i][j] = '.';
                }
            }
        }
        if (!has_place_to_put) {
            status[status_num] = 5; // draw
        }
    }
}

int main() {
    init();
    dfs();
    for (int i = 0; i < 3; i ++) cin >> grid[i];
    string ans;
    switch (status[get_status_num()]) {
        case 0: ans = "illegal"; break;
        case 1: ans = "first"; break;
        case 2: ans = "second"; break;
        case 3: ans = "the first player won"; break;
        case 4: ans = "the second player won"; break;
        case 5: ans = "draw"; break;
        default: break;
    }
    cout << ans << endl;
    return 0;
}

以上是关于Codeforces3C. Tic-tac-toe 题解 状态压缩+搜索的主要内容,如果未能解决你的问题,请参考以下文章

Tic-tac-toe使用python tkinter

python tic-tac-toe.py

简单的Tic-Tac-Toe

Design Tic-Tac-Toe 解答

Design Tic-Tac-Toe

java 348.设计Tic-Tac-Toe(#1).java