Imagine a thrilling chase game between a clever Cat and a sneaky Mouse on a connected graph! This is a classic game theory problem that tests your understanding of optimal strategy and dynamic programming.

The Setup:

  • The graph is represented as graph[a] - a list of all nodes connected to node a
  • Mouse starts at node 1 and moves first
  • Cat starts at node 2 and moves second
  • There's a safe hole at node 0 where the mouse can escape

The Rules:

  • Players alternate turns, each must move along an edge to a connected node
  • The Cat cannot enter the hole (node 0)
  • Cat wins if it catches the mouse (same node)
  • Mouse wins if it reaches the hole (node 0)
  • It's a draw if the same game state repeats

Your Goal: Assuming both players play optimally, determine the winner!

Return 1 if mouse wins, 2 if cat wins, or 0 for a draw.

Input & Output

example_1.py — Simple Triangle
$ Input: graph = [[2,5],[3],[0,4,5],[1,4,5],[2,3],[0,2,3]]
Output: 0
💡 Note: Mouse starts at 1, Cat at 2. Both players play optimally. The mouse can move to 3, and regardless of cat's moves, the game leads to a draw situation where positions repeat.
example_2.py — Mouse Can Win
$ Input: graph = [[1,3],[0,2],[1,3],[0,2]]
Output: 1
💡 Note: Mouse at 1 can immediately move to 0 (the hole) and win, regardless of what the cat does from position 2.
example_3.py — Cat Dominates
$ Input: graph = [[1,2,3],[0,2],[0,1],[0]]
Output: 2
💡 Note: The cat at position 2 has strategic advantage and can force a win by controlling key positions and eventually catching the mouse.

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

Visualization

Tap to expand
Cat and Mouse - Game Theory with BFS INPUT Connected Graph 0 HOLE 1 Mouse 2 Cat 3 4 5 graph = [[2,5],[3], [0,4,5],[1,4,5], [2,3],[0,2,3]] Mouse:1, Cat:2, Hole:0 ALGORITHM STEPS 1 Define States (mouse_pos, cat_pos, turn) 2 Init Terminal States Mouse@0=Win, Same=CatWin 3 BFS Backwards Propagate known outcomes 4 Check Start State State(1,2,mouse_turn) State Transitions State Turn Result (0,*,*) any Mouse=1 (x,x,*) any Cat=2 (1,2,M) start Draw=0 Cycle detected in game FINAL RESULT Game Analysis Complete DRAW Neither wins! Infinite loop detected M C repeat Output: 0 Optimal play = Draw Both avoid losing Key Insight: This is a minimax game solved via backward BFS. We start from terminal states (mouse at hole = mouse wins, cat catches mouse = cat wins) and propagate outcomes backward. Each player picks their optimal move. If state(1,2,mouse_turn) remains undetermined after BFS, it means neither can force a win --> DRAW (0). The graph structure creates cycles where both players can indefinitely avoid losing! TutorialsPoint - Cat and Mouse | BFS Backward Propagation
Asked in
Google 15 Amazon 8 Meta 6 Microsoft 4
28.4K Views
Medium Frequency
~45 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen