leetcode 913(2022 1.4)

const int MOUSE_WIN = 1;
const int CAT_WIN = 2;
const int DRAW = 0;
const int MAXN = 51;
class Solution {
    int n;
    int dp[MAXN * 2][MAXN][MAXN];
    vector<vector<int>> g;
public:
    int catMouseGame(vector<vector<int>>& graph) {
        n = graph.size();
        g = graph;
        memset(dp, -1, sizeof(dp));
        return dfs(0, 1, 2);
    }
    int dfs(int k, int a, int b) {
        int ans = dp[k][a][b];
        if (a == 0) ans = 1;
        else if (a == b) ans = 2;
        else if (k >= 2 * n) ans = 0;
        else if (ans == -1) {
            bool win = false, draw = false;
            if (k % 2 == 0) { // mouse
                
                for (int ne : g[a]) {
                    int t = dfs(k + 1, ne, b);
                    if (t == 1)
                    {
                        win = true;
                        break;
                    }
                    else if (t == 0)
                    {
                        draw = true;
                    }
                }
                if (win) ans = 1;
                else if (draw) ans = 0;
                else ans = 2;
            } else { // cat
                for (int ne : g[b]) {
                    if (ne == 0) continue;
                    int t = dfs(k + 1, a, ne);
                    if (t == 2) {
                        win = true;
                        break;
                    }
                    else if (t == 0) {
                        draw = true;
                    }
                }
                if (win) ans = 2;
                else if (draw) ans = 0;
                else ans = 1;
            }
        }
        dp[k][a][b] = ans;
        return ans;
    }

};

直接cv的三叶姐代码,目前的水平还驾驭不住这种难度的题
假期有时间看看动态规划~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值