A game is played by a cat and a mouse named Cat and Mouse.

The environment is represented by a grid of size rows x cols, where each element is a wall, floor, player (Cat, Mouse), or food.

  • Players are represented by the characters 'C' (Cat), 'M' (Mouse).
  • Floors are represented by the character '.' and can be walked on.
  • Walls are represented by the character '#' and cannot be walked on.
  • Food is represented by the character 'F' and can be walked on.
  • There is only one of each character 'C', 'M', and 'F' in grid.

Mouse and Cat play according to the following rules:

  • Mouse moves first, then they take turns to move.
  • During each turn, Cat and Mouse can jump in one of the four directions (left, right, up, down). They cannot jump over the wall nor outside of the grid.
  • catJump, mouseJump are the maximum lengths Cat and Mouse can jump at a time, respectively. Cat and Mouse can jump less than the maximum length.
  • Staying in the same position is allowed.
  • Mouse can jump over Cat.

The game can end in 4 ways:

  • If Cat occupies the same position as Mouse, Cat wins.
  • If Cat reaches the food first, Cat wins.
  • If Mouse reaches the food first, Mouse wins.
  • If Mouse cannot get to the food within 1000 turns, Cat wins.

Given a rows x cols matrix grid and two integers catJump and mouseJump, return true if Mouse can win the game if both Cat and Mouse play optimally, otherwise return false.

Input & Output

Example 1 — Basic Grid
$ Input: grid = [["M",".","F"],[".","#","."],[".",".","C"]], catJump = 2, mouseJump = 1
Output: true
💡 Note: Mouse can reach food in 2 moves while cat needs more moves due to wall blocking and lower jump capacity
Example 2 — Cat Advantage
$ Input: grid = [["M",".",".",".","C"],[".",".",".",".","."],[".",".","F",".","."]], catJump = 3, mouseJump = 1
Output: false
💡 Note: Cat has higher jump capacity and can intercept mouse before it reaches food
Example 3 — Wall Protection
$ Input: grid = [["C","#","M"],["#","#","#"],["F",".","."]], catJump = 1, mouseJump = 2
Output: true
💡 Note: Wall separates cat from mouse, giving mouse safe path to food with higher jump

Constraints

  • rows == grid.length
  • cols = grid[i].length
  • 1 ≤ rows, cols ≤ 8
  • grid[i][j] consist only of characters 'C', 'M', 'F', '.', '#'
  • There is only one of each character 'C', 'M', and 'F' in grid
  • 1 ≤ catJump, mouseJump ≤ 8

Visualization

Tap to expand
Cat and Mouse II: Strategic Game on GridM.F#CMouseFloorFoodWallCatGame Rules• Mouse moves first, then alternate• Each can jump 1-N cells in 4 directions• Mouse wins if reaches food first• Cat wins if catches mouse or gets food• Timeout after 1000 moves → Cat wins🎯 Key Insight: Model as minimax game with optimal play from both sides
Understanding the Visualization
1
Game Setup
Grid with cat (C), mouse (M), food (F), walls (#)
2
Turn-Based Movement
Players alternate moves with jump limits
3
Win Conditions
Mouse wins if it reaches food first, cat wins otherwise
Key Takeaway
🎯 Key Insight: Use game theory - each position has deterministic outcome with optimal play
Asked in
Google 12 Microsoft 8 Amazon 6
15.6K Views
Medium Frequency
~45 min Avg. Time
234 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