Problem Description
Two players – a Mouse and a Cat – play a game on an undirected graph. The Mouse starts at node 1 and the Cat at node 2. The graph is given in an array format where graph[a] lists all nodes b for which there is an edge a-b. The Mouse and Cat alternate moves and must move to an adjacent node with the Cat not allowed to enter the hole at node 0. The game ends when: the Cat catches the Mouse (both occupy the same node), the Mouse reaches the hole (node 0), or the game repeats a position with the same player to move (resulting in a draw). When both players play optimally, return 1 if the Mouse wins, 2 if the Cat wins, or 0 if the game is a draw.
Key Insights
- Model the game as a state-space with dp[mouse][cat][turn], where turn indicates whether it is the Mouse’s or the Cat’s move.
- Use retrograde analysis (backward induction) starting from terminal “winning” states:
- The Mouse wins if it reaches node 0.
- The Cat wins if it catches the Mouse (except when the Mouse is at the hole).
- Use topological sort/BFS in reverse over the state-space to “propagate” wins to parent states.
- Maintain for each state the number of moves (degree) available and update the state outcome only when all moves have been exhausted, ensuring that cycles are marked as draws.
- Handle the Cat’s restriction (cannot move to node 0) when computing legal moves.
Space and Time Complexity
Time Complexity: O(N^3) in the worst-case scenario, where N is the number of nodes (due to the state space being roughly O(N^2) and processing each move potentially examining O(N) neighbors). Space Complexity: O(N^2) for storing the state results over all possible (mouse, cat, turn) configurations.
Solution
We solve the problem using a dynamic programming approach combined with retrograde analysis (backward induction) on the state-space defined by the positions of the Mouse and Cat along with whose turn it is. The idea is to initialize the states with known outcomes (terminal states) and then “propagate” these outcomes backwards to all states that can reach them.
- Define the state dp[mouse][cat][turn] where turn = 0 means Mouse’s turn and turn = 1 means Cat’s turn.
- Initialize terminal states:
- If the Mouse is at node 0, the Mouse wins.
- If the Cat and Mouse are at the same node (and the Mouse is not at node 0), the Cat wins.
- For all other states, count the valid moves (degree) based on the problem’s rules (keeping in mind that the Cat cannot move to node 0).
- Use a queue to perform a reverse BFS. For a given state with a determined outcome, check all “parent” states (i.e. states that can move to the current state) and use the following rules:
- If the current outcome is a winning move for the player who would have moved in the parent state, then the parent state is also winning.
- Otherwise, decrease the degree for that parent state. When a state’s degree is exhausted (meaning all moves lead to losing positions), mark that state as a win for the opponent.
- Return the outcome for the starting state (Mouse at node 1, Cat at node 2, Mouse's turn). This technique ensures that all states are resolved under optimal play.