Welcome to an exciting game theory problem! Imagine a strategic battle between a clever mouse and a cunning cat on a grid-based playing field.

The Setup: We have a rows Γ— cols grid where:

  • 'M' represents the Mouse (starts first)
  • 'C' represents the Cat
  • 'F' represents the Food (the ultimate prize!)
  • '.' represents walkable floor tiles
  • '#' represents impassable walls

Game Rules:

  • Mouse moves first, then they alternate turns
  • Each player can jump up to mouseJump or catJump steps in any cardinal direction
  • Players can choose to stay in place or jump any distance ≀ their max jump
  • Mouse can jump over the cat (but not walls!)

Victory Conditions:

  • Cat wins if: Cat catches mouse OR cat reaches food first OR 1000+ turns pass
  • Mouse wins if: Mouse reaches food before cat and avoids capture

Your Mission: Determine if the mouse can guarantee a win with optimal play from both players.

Input & Output

example_1.py β€” Python
$ Input: grid = ["####F","#C...","M...."] catJump = 1, mouseJump = 2
β€Ί Output: true
πŸ’‘ Note: Mouse can win by jumping 2 steps right to (2,2), then the cat can only move 1 step and cannot catch up. Mouse continues toward food while staying ahead of the slower cat.
example_2.py β€” Python
$ Input: grid = ["M.C...F"] catJump = 1, mouseJump = 4
β€Ί Output: true
πŸ’‘ Note: Mouse can jump 4 spaces directly to (0,4), then 2 more to reach food at (0,6). Cat can only move 1 space per turn and cannot prevent mouse from reaching food first.
example_3.py β€” Python
$ Input: grid = ["C...#....", ".........", "#.....#..", "..M.....F"] catJump = 2, mouseJump = 5
β€Ί Output: false
πŸ’‘ Note: Even with mouse's superior jump distance, the cat can position itself strategically to either catch the mouse or reach the food first through optimal play.

Visualization

Tap to expand
Cat and Mouse Game TheoryFCMMouse path (2 jump)Cat path (1 jump)Minimax Decision TreeMouse Turn (Maximize)Cat TurnCat TurnWINLOSEWINLOSEMemoization CacheState (M:0,0 C:1,1 Turn:Mouse): CACHED_WINState (M:0,1 C:1,1 Turn:Cat): CACHED_LOSEState (M:1,0 C:1,2 Turn:Mouse): CACHED_WIN
Understanding the Visualization
1
Game State Analysis
For each position combination, determine if mouse can guarantee a win
2
Minimax Decision
Mouse chooses moves that maximize winning chances, cat minimizes them
3
Memoization Optimization
Cache results to avoid recalculating identical game states
4
Optimal Play Result
Return whether mouse can win with perfect play from both sides
Key Takeaway
🎯 Key Insight: Game theory problems require analyzing optimal play from both sides. Memoization transforms exponential minimax into efficient polynomial solution by caching computed game states.

Time & Space Complexity

Time Complexity
⏱️
O(rows Γ— cols Γ— rows Γ— cols Γ— T)

We have at most rowsΓ—cols positions for mouse, rowsΓ—cols for cat, T turns (≀1000), and 2 turn types. Each unique state is calculated once.

n
2n
βœ“ Linear Growth
Space Complexity
O(rows Γ— cols Γ— rows Γ— cols Γ— T)

Memoization cache stores results for each unique game state combination

n
2n
βœ“ Linear Space

Constraints

  • rows == grid.length
  • cols == grid[i].length
  • 1 ≀ rows, cols ≀ 8
  • grid[i][j] consist only of characters 'C', 'M', 'F', '.', and '#'
  • There is only one of each character 'C', 'M', and 'F' in grid
  • 1 ≀ catJump, mouseJump ≀ 8
  • Important: Game ends after 1000 turns if mouse hasn't won
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
23.4K Views
Medium Frequency
~35 min Avg. Time
856 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