Halloween Graveyard
Time limit1sMemory limit128 MB
Find the fastest time to travel from entrance to exit on a grid with walls and time-warping portals, detecting negative cycles and unreachability.
- Level
Medium6 of 10
- Topics
- Shortest path, Graph, BFS
- Solved
- No attempts yet
Problem
Today is Halloween. Sang-geun and his friends visited a graveyard to celebrate. They enter the graveyard one at a time, and each of them must find the way out alone. Now it is Sang-geun's turn.
When Sang-geun was little, his grandmother told him that on Halloween night, ghost holes appear in the graveyard. If you enter a ghost hole, you come back out somewhere else in the graveyard. These holes move you through time: when you fall into a ghost hole, you come out of another hole in a parallel universe after (or before) a certain amount of time.
The graveyard is a grid of size W × H. Its entrance is (0, 0) and its exit is (W-1, H-1). Sang-geun is easily frightened, so he wants to leave the graveyard as fast as possible; and the moment he reaches the exit while moving, he leaves at once without looking back. He can move from his current cell to an adjacent cell to the east, west, south, or north, and each move takes 1 second. Each cell is grass, a gravestone, or a ghost hole.
- Gravestones are very tall, so he cannot move onto a cell with a gravestone.
- When he moves onto a cell with a ghost hole, after a certain amount of time he appears somewhere else in the graveyard. This time differs for each ghost hole and can be positive, negative, or zero.
- He can move freely onto a cell with grass.
Sang-geun will also use ghost holes to leave the graveyard quickly. It is possible that he cannot leave the graveyard, or that he keeps travelling further into the past.
For example, consider a 4 × 3 graveyard with gravestones at (2, 1) and (3, 1), and a single ghost hole that you enter at (3, 0) and come out of at (2, 2) after 0 seconds. The fastest time to escape is 4 seconds, along this route:
(0, 0) → east (1 s) → (1, 0) → east (1 s) → (2, 0) → east (1 s) → (3, 0) → ghost hole (0 s) → (2, 2) → east (1 s) → (3, 2)
Without using the ghost hole, the fastest time is 5 seconds.
Input
The input consists of several test cases.
The first line of each test case contains the width W and the height H of the graveyard (). The next line contains the number of gravestones G (). Each of the next G lines contains two integers X and Y giving a gravestone's position (, ).
The next line contains the number of ghost holes E (). Each of the next E lines contains five integers X1, Y1, X2, Y2, T describing a ghost hole: (X1, Y1) is the hole's position, and (X2, Y2) is where you come out when you enter it (, ); (X1, Y1) and (X2, Y2) may be the same. T is the time it takes to come out of the hole (); a positive T means you come out after entering the hole. No two ghost holes are at the same place, and no hole's exit is on a gravestone. No gravestone or ghost hole is at (0, 0) or (W-1, H-1).
The last line of the input contains 0 0.
Output
For each test case, print Never if Sang-geun keeps travelling further and further into the past, or Impossible if he cannot reach the exit. Otherwise, print the fastest time (in seconds) in which he can leave the graveyard.