Ctrl K

Thief and Guard

Hard
A museum is an undirected graph: halls are numbered 0 to n - 1, and halls[a] lists the halls connected to hall a by a passage. Hall 0 is the exit to the street. The thief starts in hall 1 and the guard in hall 2. They take turns, the thief first. A move means walking through one passage into an adjacent hall; staying put is not allowed. The guard may never enter hall 0. The game ends as soon as: - the guard and the thief are in the same hall: the guard wins; - the thief reaches hall 0: the thief wins; - a position repeats, with both standing where they once stood before and the same player to move: it is a draw. Both play optimally. Return 1 if the thief wins, 2 if the guard wins, and 0 for a draw.
Example 1
halls=[[1], [0, 2], [1]] → 1

The thief is in hall 1 right next to the exit 0, so he leaves on his very first move.

Example 2
halls=[[3], [2], [1, 3], [0, 2]] → 2

From hall 1 the thief can only step into hall 2, where the guard is standing, and gets caught at once.

Example 3
halls=[[3], [3, 4], [3, 4], [0, 1, 2], [1, 2]] → 2
Constraints
  • 3 ≤ n = len(halls) ≤ 50
  • 1 ≤ len(halls[i]) < n
  • 0 ≤ halls[i][j] < n, halls[i][j] ≠ i, and no neighbour is listed twice
  • The graph is undirected: if b is in halls[a], then a is in halls[b]
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMouse, Cat and Cheese HardLongest Uphill Walk HardMost Repeated Colour on a Path HardRoom Building Orders HardUphill-Only Walks Hard
loading Python…
Input
halls=[[1], [0, 2], [1]]
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 45 tests on the server, hidden ones included.