Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Cat and Mouse

XPChallenge Points: 30
levelLevel: Hard

A cat and mouse are playing a turn-based game on an undirected graph.
• The graph is represented as an adjacency list:
graph[a] contains all nodes b such that there is an edge between a and b.
• The mouse starts at node 1 and moves first.
• The cat starts at node 2 and moves second.
• There is a hole at node 0, which represents a safe zone for the mouse.
________________________________________
🧩 Game Rules
1. On a player’s turn, they must move to one of the neighboring nodes connected to their current position.
2. The cat cannot move to the hole (node 0).
3. The game ends in one of three ways:
o If the mouse reaches node 0, the mouse wins.
o If the cat and mouse are on the same node, the cat wins.
o If the same position repeats (same player’s turn, same positions), the game is a draw.
________________________________________
🏁 Return
• 1 → if the mouse wins
• 2 → if the cat wins
• 0 → if the game ends in a draw
Both players play optimally to maximize their chances of winning.

Example 1:

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

Output: 1

Example 2:

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

Output: 2

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result