Skip to content

Practice Cat and Mouse II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Problem Statement

The grid contains one C (Cat), one M (Mouse), one F (food), . floor cells and # walls. Mouse moves first, then turns alternate. On a turn, a player may stay still or move in a straight cardinal direction by any positive distance up to its jump limit, without crossing walls or leaving the grid. Mouse may jump over Cat. Cat wins by reaching Mouse, reaching food first, or preventing Mouse from reaching food within 1000 turns. Mouse wins by reaching food first. Return whether Mouse can force a win under optimal play. The maximum jump distances are catJump for Cat and mouseJump for Mouse.

Source: Cat and Mouse II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
grid = ["####F","#C...","M...."], catJump = 1, mouseJump = 2
Output
true

# Example 2

Input
grid = ["M.C...F"], catJump = 1, mouseJump = 4
Output
true

# Example 3

Input
grid = ["M.C...F"], catJump = 1, mouseJump = 3
Output
false

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

Related LeetCode Problems

Practice more problems similar to Cat and Mouse II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.