|  | 
|  | 1 | +/** | 
|  | 2 | + * Title: Cat and Mouse | 
|  | 3 | + * Description: A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns. | 
|  | 4 | + * Author: Hasibul Islam | 
|  | 5 | + * Date: 06/05/2023 | 
|  | 6 | + */ | 
|  | 7 | + | 
|  | 8 | +/** | 
|  | 9 | + * @param {number[][]} graph | 
|  | 10 | + * @return {number} | 
|  | 11 | + */ | 
|  | 12 | +var catMouseGame = function (graph) { | 
|  | 13 | + const n = graph.length; | 
|  | 14 | + //using anything less than n * 5 won't pass the 89 test cases since the June 2022. As test cases get harder will need to increase this value or fix this algorithm | 
|  | 15 | + const state = new Array(n * 5) | 
|  | 16 | + .fill(0) | 
|  | 17 | + .map((el, i) => new Array(n).fill(0).map((el, i) => new Array(n).fill(-1))); | 
|  | 18 | + | 
|  | 19 | + function updateState(step, cat, mouse, value) { | 
|  | 20 | + state[step][cat][mouse] = value; | 
|  | 21 | + return state[step][cat][mouse]; | 
|  | 22 | + } | 
|  | 23 | + | 
|  | 24 | + function dfs(step, cat, mouse) { | 
|  | 25 | + if (step >= state.length) return 0; | 
|  | 26 | + if (state[step][cat][mouse] !== -1) { | 
|  | 27 | + return state[step][cat][mouse]; | 
|  | 28 | + } | 
|  | 29 | + let finalResult = -1; | 
|  | 30 | + if (step & 1) { | 
|  | 31 | + //cat's turn | 
|  | 32 | + if (graph[cat].includes(mouse)) { | 
|  | 33 | + return (state[step][cat][mouse] = 2); | 
|  | 34 | + } | 
|  | 35 | + for (const node of graph[cat]) { | 
|  | 36 | + if (node !== 0) { | 
|  | 37 | + let nextResult = dfs(step + 1, node, mouse, 0); | 
|  | 38 | + if (nextResult === 2) { | 
|  | 39 | + return (state[step][cat][mouse] = 2); | 
|  | 40 | + } else if (nextResult === 1) { | 
|  | 41 | + if (finalResult === -1) { | 
|  | 42 | + finalResult = 1; | 
|  | 43 | + } | 
|  | 44 | + } else if (nextResult === 0) { | 
|  | 45 | + finalResult = 0; | 
|  | 46 | + } | 
|  | 47 | + } | 
|  | 48 | + } | 
|  | 49 | + if (finalResult === -1) { | 
|  | 50 | + finalResult = 1; | 
|  | 51 | + } | 
|  | 52 | + } else { | 
|  | 53 | + //mouse's turn | 
|  | 54 | + if (graph[mouse].includes(0)) { | 
|  | 55 | + return (state[step][cat][mouse] = 1); | 
|  | 56 | + } | 
|  | 57 | + for (const node of graph[mouse]) { | 
|  | 58 | + if (node !== cat && !graph[node].includes(cat)) { | 
|  | 59 | + let nextResult = dfs(step + 1, cat, node, 1); | 
|  | 60 | + if (nextResult === 1) { | 
|  | 61 | + return (state[step][cat][mouse] = 1); | 
|  | 62 | + } else if (nextResult === 2) { | 
|  | 63 | + if (finalResult === -1) { | 
|  | 64 | + finalResult = 2; | 
|  | 65 | + } | 
|  | 66 | + } else if (nextResult === 0) { | 
|  | 67 | + finalResult = 0; | 
|  | 68 | + } | 
|  | 69 | + } | 
|  | 70 | + } | 
|  | 71 | + if (finalResult === -1) { | 
|  | 72 | + finalResult = 2; | 
|  | 73 | + } | 
|  | 74 | + } | 
|  | 75 | + return (state[step][cat][mouse] = finalResult); | 
|  | 76 | + } | 
|  | 77 | + return dfs(0, 2, 1); | 
|  | 78 | +}; | 
0 commit comments