Cat and Mouse II - Problem
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
mouseJumporcatJumpsteps 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
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.
β Linear Growth
Space Complexity
O(rows Γ cols Γ rows Γ cols Γ T)
Memoization cache stores results for each unique game state combination
β 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
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code