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 - Game Theory INPUT M F # C Mouse (M) Cat (C) Food (F) Wall (#) catJump = 2 mouseJump = 1 Grid: 3 x 3 ALGORITHM STEPS 1 Define State (mousePos, catPos, turn, moves) 2 BFS/DFS + Memoization Cache game states visited 3 Simulate Turns Mouse moves first, alternate 4 Minimax Logic Mouse maximizes, Cat minimizes Game Tree M C C W FINAL RESULT F C Mouse reaches food in 2 moves Cat cannot intercept (too far) Optimal Play: Turn 1: M(0,0) --> (0,1) Turn 2: C(2,2) --> (2,0) Turn 3: M(0,1) --> (0,2)=F Output: true Key Insight: This is a game theory problem solved using minimax with memoization. The state space includes positions of both players, whose turn it is, and move count. Mouse wins if it can reach food before cat catches it or reaches food. With mouseJump=1 and catJump=2, mouse can still win optimally. TutorialsPoint - Cat and Mouse II | Minimax with Memoization Approach
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