913. Cat and Mouse Hard

@problem@discussion
#Math#Dynamic Programming#Graph#Topological Sort#Memoization#Game Theory



1/**
2 * [913] Cat and Mouse
3 *
4 * A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns.
5 * The graph is given as follows: graph[a] is a list of all nodes b such that ab is an edge of the graph.
6 * 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.
7 * 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].
8 * Additionally, it is not allowed for the Cat to travel to the Hole (node 0.)
9 * Then, the game can end in three ways:
10 * 
11 * 	If ever the Cat occupies the same node as the Mouse, the Cat wins.
12 * 	If ever the Mouse reaches the Hole, the Mouse wins.
13 * 	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.
14 * 
15 * Given a graph, and assuming both players play optimally, return
16 * 
17 * 	1 if the mouse wins the game,
18 * 	2 if the cat wins the game, or
19 * 	0 if the game is a draw.
20 * 
21 *  
22 * Example 1:
23 * <img alt="" src="https://assets.leetcode.com/uploads/2020/11/17/cat1.jpg" style="width: 300px; height: 300px;" />
24 * Input: graph = [[2,5],[3],[0,4,5],[1,4,5],[2,3],[0,2,3]]
25 * Output: 0
26 * 
27 * Example 2:
28 * <img alt="" src="https://assets.leetcode.com/uploads/2020/11/17/cat2.jpg" style="width: 200px; height: 200px;" />
29 * Input: graph = [[1,3],[0],[3],[0,2]]
30 * Output: 1
31 * 
32 *  
33 * Constraints:
34 * 
35 * 	3 <= graph.length <= 50
36 * 	1 <= graph[i].length < graph.length
37 * 	0 <= graph[i][j] < graph.length
38 * 	graph[i][j] != i
39 * 	graph[i] is unique.
40 * 	The mouse and the cat can always move. 
41 * 
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/cat-and-mouse/
46// discuss: https://leetcode.com/problems/cat-and-mouse/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51    pub fn cat_mouse_game(graph: Vec<Vec<i32>>) -> i32 {
52        0
53    }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_913() {
64    }
65}
66


Back
© 2025 bowen.ge All Rights Reserved.