|
| 1 | +/** |
| 2 | + * 913. Cat and Mouse |
| 3 | + * https://leetcode.com/problems/cat-and-mouse/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns. |
| 7 | + * |
| 8 | + * The graph is given as follows: graph[a] is a list of all nodes b such that ab is an edge of |
| 9 | + * the graph. |
| 10 | + * |
| 11 | + * The mouse starts at node 1 and goes first, the cat starts at node 2 and goes second, and |
| 12 | + * there is a hole at node 0. |
| 13 | + * |
| 14 | + * During each player's turn, they must travel along one edge of the graph that meets where they |
| 15 | + * are. For example, if the Mouse is at node 1, it must travel to any node in graph[1]. |
| 16 | + * |
| 17 | + * Additionally, it is not allowed for the Cat to travel to the Hole (node 0). |
| 18 | + * |
| 19 | + * Then, the game can end in three ways: |
| 20 | + * - If ever the Cat occupies the same node as the Mouse, the Cat wins. |
| 21 | + * - If ever the Mouse reaches the Hole, the Mouse wins. |
| 22 | + * - If ever a position is repeated (i.e., the players are in the same position as a previous |
| 23 | + * turn, and it is the same player's turn to move), the game is a draw. |
| 24 | + * |
| 25 | + * Given a graph, and assuming both players play optimally, return |
| 26 | + * - 1 if the mouse wins the game, |
| 27 | + * - 2 if the cat wins the game, or |
| 28 | + * - 0 if the game is a draw. |
| 29 | + */ |
| 30 | + |
| 31 | +/** |
| 32 | + * @param {number[][]} graph |
| 33 | + * @return {number} |
| 34 | + */ |
| 35 | +var catMouseGame = function(graph) { |
| 36 | + const MOUSE_TURN = 0; |
| 37 | + const CAT_TURN = 1; |
| 38 | + const MOUSE_WIN = 1; |
| 39 | + const CAT_WIN = 2; |
| 40 | + const n = graph.length; |
| 41 | + |
| 42 | + const color = new Array(n).fill().map(() => { |
| 43 | + return new Array(n).fill().map(() => new Array(2).fill(0)); |
| 44 | + }); |
| 45 | + |
| 46 | + const degree = new Array(n).fill().map(() => { |
| 47 | + return new Array(n).fill().map(() => new Array(2).fill(0)); |
| 48 | + }); |
| 49 | + |
| 50 | + const queue = []; |
| 51 | + |
| 52 | + for (let i = 0; i < n; i++) { |
| 53 | + for (let turn = 0; turn < 2; turn++) { |
| 54 | + color[0][i][turn] = MOUSE_WIN; |
| 55 | + queue.push([0, i, turn, MOUSE_WIN]); |
| 56 | + |
| 57 | + if (i > 0) { |
| 58 | + color[i][i][turn] = CAT_WIN; |
| 59 | + queue.push([i, i, turn, CAT_WIN]); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + for (let m = 0; m < n; m++) { |
| 65 | + for (let c = 0; c < n; c++) { |
| 66 | + degree[m][c][MOUSE_TURN] = graph[m].length; |
| 67 | + degree[m][c][CAT_TURN] = graph[c].length; |
| 68 | + |
| 69 | + for (let x = 0; x < graph[c].length; x++) { |
| 70 | + if (graph[c][x] === 0) { |
| 71 | + degree[m][c][CAT_TURN]--; |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + while (queue.length > 0) { |
| 79 | + const [mouse, cat, turn, result] = queue.shift(); |
| 80 | + |
| 81 | + const prevTurn = 1 - turn; |
| 82 | + const prevPositions = []; |
| 83 | + |
| 84 | + if (prevTurn === MOUSE_TURN) { |
| 85 | + for (const prevMouse of graph[mouse]) { |
| 86 | + prevPositions.push([prevMouse, cat]); |
| 87 | + } |
| 88 | + } else { |
| 89 | + for (const prevCat of graph[cat]) { |
| 90 | + if (prevCat !== 0) { |
| 91 | + prevPositions.push([mouse, prevCat]); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + for (const [prevMouse, prevCat] of prevPositions) { |
| 97 | + if (color[prevMouse][prevCat][prevTurn] !== 0) continue; |
| 98 | + |
| 99 | + if ((prevTurn === MOUSE_TURN && result === MOUSE_WIN) |
| 100 | + || (prevTurn === CAT_TURN && result === CAT_WIN)) { |
| 101 | + color[prevMouse][prevCat][prevTurn] = result; |
| 102 | + queue.push([prevMouse, prevCat, prevTurn, result]); |
| 103 | + } else { |
| 104 | + degree[prevMouse][prevCat][prevTurn]--; |
| 105 | + if (degree[prevMouse][prevCat][prevTurn] === 0) { |
| 106 | + color[prevMouse][prevCat][prevTurn] = result; |
| 107 | + queue.push([prevMouse, prevCat, prevTurn, result]); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return color[1][2][MOUSE_TURN]; |
| 114 | +}; |
0 commit comments