Meteor Shower
InterviewTime limit1sMemory limit128 MB
Find the earliest time for a unit-speed mover to reach any lattice point that no meteor ever destroys, given each meteor's impact time and cross-shaped destruction.
- Level
Medium6 of 10
- Topics
- BFS, Graph, Simulation
- Solved
- No attempts yet
Problem
Bessie hears that an extraordinary meteor shower is coming: reports say the meteors will crash into the ground and destroy anything they hit. Worried for her safety, she resolves to reach a safe location — a lattice point that is never destroyed by any meteor.
She starts grazing at the origin of the coordinate plane and wants to move to a safer spot while avoiding the meteors along the way.
The reports say that meteors will strike. Meteor hits the point at time . Each meteor destroys the point it strikes together with the four rectilinearly adjacent lattice points (up, down, left, and right).
Bessie leaves the origin at time . She stays in the first quadrant (all coordinates ) and moves parallel to the axes at a rate of one unit of distance per second, stepping to any adjacent lattice point that has not yet been destroyed. She can never occupy a point at a time greater than or equal to the moment that point is destroyed.
Determine the minimum time Bessie needs to reach a safe point, or report that it is impossible.
Constraints: , , , .
Input
- Line 1: a single integer .
- Lines 2 to : line contains three space-separated integers , , and .
Output
- Line 1: the minimum time it takes Bessie to reach a safe point, or if it is impossible.
Notes
In the sample there are four meteors, striking points , , , and at times , , , and respectively.
t = 0 t = 2 t = 5
5|. . . . . . . 5|. . . . . . . 5|. . . . . . .
4|. . . . . . . 4|. . . . . . . 4|# . . . . . . * = meteor impact
3|. . . . . . . 3|. . . . . . . 3|* # . . . . .
2|. . . . . . . 2|. # # . . . . 2|# # # . . . . # = destroyed pasture
1|. . . . . . . 1|# * * # . . . 1|# # # # . . .
0|B . . . . . . 0|* # # . . . . 0|# # # . . . .
-------------- -------------- --------------
0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6
At the closest safe point is , but Bessie's path there is blocked too quickly by the second meteor. The next closest, , is also blocked too soon. After that come the lattice points on the diagonal from to ; of those, any one of , , and can be reached in time units.
5|. . . . . . .
4|. . . . . . .
3|3 4 5 . . . . Bessie's positions over time
2|2 . . . . . . for one solution
1|1 . . . . . .
0|0 . . . . . .
--------------
0 1 2 3 4 5 6