Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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.
'C'(Cat),'M'(Mouse).'.' and can be walked on.'#' and cannot be walked on.'F' and can be walked on.'C', 'M', and 'F' in grid.Mouse and Cat play according to the following rules:
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.The game can end in 4 ways:
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.
Example 1:
Input: grid = ["####F","#C...","M...."], catJump = 1, mouseJump = 2 Output: true Explanation: Cat cannot catch Mouse on its turn nor can it get the food before Mouse.
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.lengthcols = grid[i].length1 <= rows, cols <= 8grid[i][j] consist only of characters 'C', 'M', 'F', '.', and '#'.'C', 'M', and 'F' in grid.1 <= catJump, mouseJump <= 8Problem summary: 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
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Dynamic Programming · Topological Sort
["####F","#C...","M...."] 1 2
["M.C...F"] 1 4
["M.C...F"] 1 3
escape-the-ghosts)cat-and-mouse)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1728: Cat and Mouse II
class Solution {
private final int[] dirs = {-1, 0, 1, 0, -1};
public boolean canMouseWin(String[] grid, int catJump, int mouseJump) {
int m = grid.length;
int n = grid[0].length();
int catStart = 0, mouseStart = 0, food = 0;
List<Integer>[] gMouse = new List[m * n];
List<Integer>[] gCat = new List[m * n];
Arrays.setAll(gMouse, i -> new ArrayList<>());
Arrays.setAll(gCat, i -> new ArrayList<>());
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
char c = grid[i].charAt(j);
if (c == '#') {
continue;
}
int v = i * n + j;
if (c == 'C') {
catStart = v;
} else if (c == 'M') {
mouseStart = v;
} else if (c == 'F') {
food = v;
}
for (int d = 0; d < 4; ++d) {
for (int k = 0; k <= mouseJump; k++) {
int x = i + k * dirs[d];
int y = j + k * dirs[d + 1];
if (x < 0 || x >= m || y < 0 || y >= n || grid[x].charAt(y) == '#') {
break;
}
gMouse[v].add(x * n + y);
}
for (int k = 0; k <= catJump; k++) {
int x = i + k * dirs[d];
int y = j + k * dirs[d + 1];
if (x < 0 || x >= m || y < 0 || y >= n || grid[x].charAt(y) == '#') {
break;
}
gCat[v].add(x * n + y);
}
}
}
}
return calc(gMouse, gCat, mouseStart, catStart, food) == 1;
}
private int calc(
List<Integer>[] gMouse, List<Integer>[] gCat, int mouseStart, int catStart, int hole) {
int n = gMouse.length;
int[][][] degree = new int[n][n][2];
int[][][] ans = new int[n][n][2];
Deque<int[]> q = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
degree[i][j][0] = gMouse[i].size();
degree[i][j][1] = gCat[j].size();
}
}
for (int i = 0; i < n; i++) {
ans[hole][i][1] = 1;
ans[i][hole][0] = 2;
ans[i][i][1] = 2;
ans[i][i][0] = 2;
q.offer(new int[] {hole, i, 1});
q.offer(new int[] {i, hole, 0});
q.offer(new int[] {i, i, 0});
q.offer(new int[] {i, i, 1});
}
while (!q.isEmpty()) {
int[] state = q.poll();
int m = state[0], c = state[1], t = state[2];
int result = ans[m][c][t];
for (int[] prevState : getPrevStates(gMouse, gCat, state, ans)) {
int pm = prevState[0], pc = prevState[1], pt = prevState[2];
if (pt == result - 1) {
ans[pm][pc][pt] = result;
q.offer(prevState);
} else {
degree[pm][pc][pt]--;
if (degree[pm][pc][pt] == 0) {
ans[pm][pc][pt] = result;
q.offer(prevState);
}
}
}
}
return ans[mouseStart][catStart][0];
}
private List<int[]> getPrevStates(
List<Integer>[] gMouse, List<Integer>[] gCat, int[] state, int[][][] ans) {
int m = state[0], c = state[1], t = state[2];
int pt = t ^ 1;
List<int[]> pre = new ArrayList<>();
if (pt == 1) {
for (int pc : gCat[c]) {
if (ans[m][pc][1] == 0) {
pre.add(new int[] {m, pc, pt});
}
}
} else {
for (int pm : gMouse[m]) {
if (ans[pm][c][0] == 0) {
pre.add(new int[] {pm, c, 0});
}
}
}
return pre;
}
}
// Accepted solution for LeetCode #1728: Cat and Mouse II
func canMouseWin(grid []string, catJump int, mouseJump int) bool {
m, n := len(grid), len(grid[0])
catStart, mouseStart, food := 0, 0, 0
dirs := []int{-1, 0, 1, 0, -1}
gMouse := make([][]int, m*n)
gCat := make([][]int, m*n)
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
c := grid[i][j]
if c == '#' {
continue
}
v := i*n + j
if c == 'C' {
catStart = v
} else if c == 'M' {
mouseStart = v
} else if c == 'F' {
food = v
}
for d := 0; d < 4; d++ {
a, b := dirs[d], dirs[d+1]
for k := 0; k <= mouseJump; k++ {
x, y := i+k*a, j+k*b
if !(0 <= x && x < m && 0 <= y && y < n && grid[x][y] != '#') {
break
}
gMouse[v] = append(gMouse[v], x*n+y)
}
for k := 0; k <= catJump; k++ {
x, y := i+k*a, j+k*b
if !(0 <= x && x < m && 0 <= y && y < n && grid[x][y] != '#') {
break
}
gCat[v] = append(gCat[v], x*n+y)
}
}
}
}
return calc(gMouse, gCat, mouseStart, catStart, food) == 1
}
func calc(gMouse, gCat [][]int, mouseStart, catStart, hole int) int {
n := len(gMouse)
degree := make([][][]int, n)
ans := make([][][]int, n)
for i := 0; i < n; i++ {
degree[i] = make([][]int, n)
ans[i] = make([][]int, n)
for j := 0; j < n; j++ {
degree[i][j] = make([]int, 2)
ans[i][j] = make([]int, 2)
degree[i][j][0] = len(gMouse[i])
degree[i][j][1] = len(gCat[j])
}
}
q := list.New()
for i := 0; i < n; i++ {
ans[hole][i][1] = 1
ans[i][hole][0] = 2
ans[i][i][1] = 2
ans[i][i][0] = 2
q.PushBack([]int{hole, i, 1})
q.PushBack([]int{i, hole, 0})
q.PushBack([]int{i, i, 0})
q.PushBack([]int{i, i, 1})
}
for q.Len() > 0 {
front := q.Front()
q.Remove(front)
state := front.Value.([]int)
m, c, t := state[0], state[1], state[2]
currentAns := ans[m][c][t]
for _, prevState := range getPrevStates(gMouse, gCat, m, c, t, ans) {
pm, pc, pt := prevState[0], prevState[1], prevState[2]
if pt == currentAns-1 {
ans[pm][pc][pt] = currentAns
q.PushBack([]int{pm, pc, pt})
} else {
degree[pm][pc][pt]--
if degree[pm][pc][pt] == 0 {
ans[pm][pc][pt] = currentAns
q.PushBack([]int{pm, pc, pt})
}
}
}
}
return ans[mouseStart][catStart][0]
}
func getPrevStates(gMouse, gCat [][]int, m, c, t int, ans [][][]int) [][]int {
pt := t ^ 1
pre := [][]int{}
if pt == 1 {
for _, pc := range gCat[c] {
if ans[m][pc][1] == 0 {
pre = append(pre, []int{m, pc, pt})
}
}
} else {
for _, pm := range gMouse[m] {
if ans[pm][c][0] == 0 {
pre = append(pre, []int{pm, c, pt})
}
}
}
return pre
}
# Accepted solution for LeetCode #1728: Cat and Mouse II
class Solution:
def canMouseWin(self, grid: List[str], catJump: int, mouseJump: int) -> bool:
m, n = len(grid), len(grid[0])
cat_start = mouse_start = food = 0
dirs = (-1, 0, 1, 0, -1)
g_mouse = [[] for _ in range(m * n)]
g_cat = [[] for _ in range(m * n)]
for i, row in enumerate(grid):
for j, c in enumerate(row):
if c == "#":
continue
v = i * n + j
if c == "C":
cat_start = v
elif c == "M":
mouse_start = v
elif c == "F":
food = v
for a, b in pairwise(dirs):
for k in range(mouseJump + 1):
x, y = i + k * a, j + k * b
if not (0 <= x < m and 0 <= y < n and grid[x][y] != "#"):
break
g_mouse[v].append(x * n + y)
for k in range(catJump + 1):
x, y = i + k * a, j + k * b
if not (0 <= x < m and 0 <= y < n and grid[x][y] != "#"):
break
g_cat[v].append(x * n + y)
return self.calc(g_mouse, g_cat, mouse_start, cat_start, food) == 1
def calc(
self,
g_mouse: List[List[int]],
g_cat: List[List[int]],
mouse_start: int,
cat_start: int,
hole: int,
) -> int:
def get_prev_states(state):
m, c, t = state
pt = t ^ 1
pre = []
if pt == 1:
for pc in g_cat[c]:
if ans[m][pc][1] == 0:
pre.append((m, pc, pt))
else:
for pm in g_mouse[m]:
if ans[pm][c][0] == 0:
pre.append((pm, c, 0))
return pre
n = len(g_mouse)
degree = [[[0, 0] for _ in range(n)] for _ in range(n)]
for i in range(n):
for j in range(n):
degree[i][j][0] = len(g_mouse[i])
degree[i][j][1] = len(g_cat[j])
ans = [[[0, 0] for _ in range(n)] for _ in range(n)]
q = deque()
for i in range(n):
ans[hole][i][1] = 1
ans[i][hole][0] = 2
ans[i][i][1] = ans[i][i][0] = 2
q.append((hole, i, 1))
q.append((i, hole, 0))
q.append((i, i, 0))
q.append((i, i, 1))
while q:
state = q.popleft()
t = ans[state[0]][state[1]][state[2]]
for prev_state in get_prev_states(state):
pm, pc, pt = prev_state
if pt == t - 1:
ans[pm][pc][pt] = t
q.append(prev_state)
else:
degree[pm][pc][pt] -= 1
if degree[pm][pc][pt] == 0:
ans[pm][pc][pt] = t
q.append(prev_state)
return ans[mouse_start][cat_start][0]
// Accepted solution for LeetCode #1728: Cat and Mouse II
/**
* [1728] Cat and Mouse II
*
* 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.
*
* Example 1:
* <img alt="" src="https://assets.leetcode.com/uploads/2020/09/12/sample_111_1955.png" style="width: 580px; height: 239px;" />
* Input: grid = ["####F","#C...","M...."], catJump = 1, mouseJump = 2
* Output: true
* Explanation: Cat cannot catch Mouse on its turn nor can it get the food before Mouse.
*
* Example 2:
* <img alt="" src="https://assets.leetcode.com/uploads/2020/09/12/sample_2_1955.png" style="width: 580px; height: 175px;" />
* 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
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/cat-and-mouse-ii/
// discuss: https://leetcode.com/problems/cat-and-mouse-ii/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn can_mouse_win(grid: Vec<String>, cat_jump: i32, mouse_jump: i32) -> bool {
// TODO
false
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_1728_example_1() {
let grid = vec_string!["####F", "#C...", "M...."];
let cat_jump = 1;
let mouse_jump = 2;
let result = true;
assert_eq!(Solution::can_mouse_win(grid, cat_jump, mouse_jump), result);
}
#[test]
#[ignore]
fn test_1728_example_2() {
let grid = vec_string!["M.C...F"];
let cat_jump = 1;
let mouse_jump = 4;
let result = true;
assert_eq!(Solution::can_mouse_win(grid, cat_jump, mouse_jump), result);
}
#[test]
#[ignore]
fn test_1728_example_3() {
let grid = vec_string!["M.C...F"];
let cat_jump = 1;
let mouse_jump = 3;
let result = false;
assert_eq!(Solution::can_mouse_win(grid, cat_jump, mouse_jump), result);
}
}
// Accepted solution for LeetCode #1728: Cat and Mouse II
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1728: Cat and Mouse II
// class Solution {
// private final int[] dirs = {-1, 0, 1, 0, -1};
//
// public boolean canMouseWin(String[] grid, int catJump, int mouseJump) {
// int m = grid.length;
// int n = grid[0].length();
// int catStart = 0, mouseStart = 0, food = 0;
// List<Integer>[] gMouse = new List[m * n];
// List<Integer>[] gCat = new List[m * n];
// Arrays.setAll(gMouse, i -> new ArrayList<>());
// Arrays.setAll(gCat, i -> new ArrayList<>());
//
// for (int i = 0; i < m; i++) {
// for (int j = 0; j < n; j++) {
// char c = grid[i].charAt(j);
// if (c == '#') {
// continue;
// }
// int v = i * n + j;
// if (c == 'C') {
// catStart = v;
// } else if (c == 'M') {
// mouseStart = v;
// } else if (c == 'F') {
// food = v;
// }
//
// for (int d = 0; d < 4; ++d) {
// for (int k = 0; k <= mouseJump; k++) {
// int x = i + k * dirs[d];
// int y = j + k * dirs[d + 1];
// if (x < 0 || x >= m || y < 0 || y >= n || grid[x].charAt(y) == '#') {
// break;
// }
// gMouse[v].add(x * n + y);
// }
// for (int k = 0; k <= catJump; k++) {
// int x = i + k * dirs[d];
// int y = j + k * dirs[d + 1];
// if (x < 0 || x >= m || y < 0 || y >= n || grid[x].charAt(y) == '#') {
// break;
// }
// gCat[v].add(x * n + y);
// }
// }
// }
// }
//
// return calc(gMouse, gCat, mouseStart, catStart, food) == 1;
// }
//
// private int calc(
// List<Integer>[] gMouse, List<Integer>[] gCat, int mouseStart, int catStart, int hole) {
// int n = gMouse.length;
// int[][][] degree = new int[n][n][2];
// int[][][] ans = new int[n][n][2];
// Deque<int[]> q = new ArrayDeque<>();
//
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++) {
// degree[i][j][0] = gMouse[i].size();
// degree[i][j][1] = gCat[j].size();
// }
// }
//
// for (int i = 0; i < n; i++) {
// ans[hole][i][1] = 1;
// ans[i][hole][0] = 2;
// ans[i][i][1] = 2;
// ans[i][i][0] = 2;
// q.offer(new int[] {hole, i, 1});
// q.offer(new int[] {i, hole, 0});
// q.offer(new int[] {i, i, 0});
// q.offer(new int[] {i, i, 1});
// }
//
// while (!q.isEmpty()) {
// int[] state = q.poll();
// int m = state[0], c = state[1], t = state[2];
// int result = ans[m][c][t];
// for (int[] prevState : getPrevStates(gMouse, gCat, state, ans)) {
// int pm = prevState[0], pc = prevState[1], pt = prevState[2];
// if (pt == result - 1) {
// ans[pm][pc][pt] = result;
// q.offer(prevState);
// } else {
// degree[pm][pc][pt]--;
// if (degree[pm][pc][pt] == 0) {
// ans[pm][pc][pt] = result;
// q.offer(prevState);
// }
// }
// }
// }
//
// return ans[mouseStart][catStart][0];
// }
//
// private List<int[]> getPrevStates(
// List<Integer>[] gMouse, List<Integer>[] gCat, int[] state, int[][][] ans) {
// int m = state[0], c = state[1], t = state[2];
// int pt = t ^ 1;
// List<int[]> pre = new ArrayList<>();
// if (pt == 1) {
// for (int pc : gCat[c]) {
// if (ans[m][pc][1] == 0) {
// pre.add(new int[] {m, pc, pt});
// }
// }
// } else {
// for (int pm : gMouse[m]) {
// if (ans[pm][c][0] == 0) {
// pre.add(new int[] {pm, c, 0});
// }
// }
// }
// return pre;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.