Skip to content

Latest commit

 

History

History
113 lines (91 loc) · 3.51 KB

913. Cat and Mouse.js.md

File metadata and controls

113 lines (91 loc) · 3.51 KB

A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns.

The graph is given as follows: graph[a] is a list of all nodes b such that ab is an edge of the graph.

The mouse starts at node 1 and goes first, the cat starts at node 2 and goes second, and there is a hole at node 0.

During each player's turn, they must travel along one edge of the graph that meets where they are. For example, if the Mouse is at node 1, it must travel to any node in graph[1].

Additionally, it is not allowed for the Cat to travel to the Hole (node 0.)

Then, the game can end in three ways:

  • If ever the Cat occupies the same node as the Mouse, the Cat wins.
  • If ever the Mouse reaches the Hole, the Mouse wins.
  • If ever a position is repeated (i.e., the players are in the same position as a previous turn, and it is the same player's turn to move), the game is a draw.

Given a graph, and assuming both players play optimally, return

  • 1 if the mouse wins the game,
  • 2 if the cat wins the game, or
  • 0 if the game is a draw.

Example 1:

Input: graph = [[2,5],[3],[0,4,5],[1,4,5],[2,3],[0,2,3]]
Output: 0

Example 2:

Input: graph = [[1,3],[0],[3],[0,2]]
Output: 1

Constraints:

  • 3 <= graph.length <= 50
  • 1 <= graph[i].length < graph.length
  • 0 <= graph[i][j] < graph.length
  • graph[i][j] != i
  • graph[i] is unique.
  • The mouse and the cat can always move.

Solution: 动态规划 (Dynamic Programming)

/**
 * @param {number[][]} graph
 * @return {number}
 */
var catMouseGame = function (graph) {
    const n = graph.length;
    const MOUSE_WIN = 1;
    const CAT_WIN = 2;
    const DRAW = 0;
    const dp = Array.from(new Array(n), () => Array.from(new Array(n), () => new Array(2 * n + 1).fill(-1)));
    return dfs(1, 2, 0);

    function dfs(mouse, cat, turns) {
        if (dp[mouse][cat][turns] < 0) {
            if (turns >= 2 * n) {
                dp[mouse][cat][turns] = DRAW;
            } else if (mouse === 0) {
                dp[mouse][cat][turns] = MOUSE_WIN;
            } else if (mouse === cat) {
                dp[mouse][cat][turns] = CAT_WIN;
            } else if (turns % 2 === 0) {
                //mouse
                let ans = CAT_WIN;
                for (let i = 0; i < graph[mouse].length; i++) {
                    let next = dfs(graph[mouse][i], cat, turns + 1);
                    if (next !== CAT_WIN) {
                        ans = next;
                    }
                    if (ans === MOUSE_WIN) {
                        break;
                    }
                }
                dp[mouse][cat][turns] = ans;
            } else {
                //cat
                let ans = MOUSE_WIN;
                for (let i = 0; i < graph[cat].length; i++) {
                    if (graph[cat][i] === 0) {
                        continue;
                    }
                    let next = dfs(mouse, graph[cat][i], turns + 1);
                    if (next !== MOUSE_WIN) {
                        ans = next;
                    }
                    if (ans === CAT_WIN) {
                        break;
                    }
                }
                dp[mouse][cat][turns] = ans;
            }
        }
        return dp[mouse][cat][turns];
    }
};