1061. Chase on a Graph
A courier and a guard move on an undirected map of n junctions numbered 0..n-1; graph[u] lists the junctions adjacent to u. Junction 0 is the exit. The courier starts at junction 1, the guard at junction 2, and they alternate turns with the courier first. On a turn the mover must travel along one road to an adjacent junction, except that the guard may never enter junction 0.
The courier wins on reaching junction 0; the guard wins on standing at the courier's junction (after either move). If the same position with the same player to move ever repeats, the game is a draw, and a player with no legal move also leads to a draw. With both sides playing optimally return 1 for a courier win, 2 for a guard win and 0 for a draw. Aim for time proportional to the number of (positions x roads).
Example 1
- Input:
- graph = [[1,2],[0,2],[0,1]]
- Output:
- 1
- Explanation:
The courier at node 1 is adjacent to the exit 0 and steps onto it on the very first move, so the courier wins (1).
Example 2
- Input:
- graph = [[3],[2],[1,3],[0,2]]
- Output:
- 2
- Explanation:
The courier's only neighbour is node 2, which the guard occupies, so the forced move gets the courier caught and the guard wins (2).
Example 3
- Input:
- graph = [[3],[2,4],[1,3,5],[0,2,4],[1,3],[2]]
- Output:
- 0
- Explanation:
The exit hangs off junction 3, which the guard can always step onto when the courier approaches, while the courier can never be cornered on the square 1-2-3-4, so positions repeat forever and the result is a draw (0).
Constraints
3 ≤ n = graph.length ≤ 300
Roads are symmetric; graph[u] has no duplicates and does not contain u; the maps are connected
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 6,000 msC++ 1,500 msJava 3,000 msJavaScript 3,000 msTypeScript 3,000 ms
Expected complexity
- Time
- O(n^2 * deg)
- Space
- O(n^2)