Skip to content

Practice Cat and Mouse

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 input graph is an adjacency list: graph[u] lists every node adjacent to u. Mouse and Cat alternate moves on the undirected graph, starting at nodes 1 and 2 respectively, with Mouse first. Each move follows one edge. Node 0 is a hole that Cat may not enter. Mouse wins by reaching node 0; Cat wins by occupying the same node as Mouse. If both positions and the player whose turn comes next repeat an earlier state, the game is a draw. Assuming optimal play, return 1 for a Mouse win, 2 for a Cat win or 0 for a draw.

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

Examples

# Example 1

Input
graph = [[2,5],[3],[0,4,5],[1,4,5],[2,3],[0,2,3]]
Output
0

# Example 2

Input
graph = [[1,3],[0],[3],[0,2]]
Output
1

Constraints

3 <= graph.length <= 50
1 <= graph[i].length < graph.length
0 <= graph[i][j] < graph.length
graph[i][j] != i
graph[i] is unique.
The mouse and the cat can always move.

Related LeetCode Problems

Practice more problems similar to Cat and Mouse.

Review your practice with PhantomCodeAI

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