Astronaut Jeongmin
Time limit1sMemory limit1024 MB
Find the fewest seconds for Jeongmin to reach the spaceship cell across two grids while avoiding black holes that spread along the airflow.
- Level
Medium7 of 10
- Topics
- BFS, Simulation, Implementation
- Solved
- No attempts yet
Problem
Astronaut Jeongmin is traveling in a world with two distinct dimensions. Each dimension is a grid whose width and height are both even, and each cell may contain a black hole. Anything in a cell with a black hole is sucked in and can never come out.
One day Jeongmin shouted "It's fine, I won't die~" and stepped outside the spaceship, then drifted away. Jeongmin is currently at in the dimension. The spaceship is parked at in the other dimension, which is . Each second, Jeongmin can move one cell up, down, left, or right, or activate a dimension-travel gate. Black holes are generated every second according to the airflow. A new black hole appears only after Jeongmin's action for that second is finished.
The airflow moves toward larger column indices when the row index is even, and toward smaller column indices when the row index is odd. When it cannot move further horizontally, it moves down one row. From it wraps to and cycles the same way. For example, with and , the cycle goes as shown below.

In a dimension with one black hole at , a new black hole appears at after 1 second, following the airflow. After 6 seconds a new black hole appears at , and after 15 seconds every cell has a black hole. No new black hole is created on a cell that already has one.

Jeongmin can cross between the two dimensions through dimension-travel gates. A gate exists in each dimension and occupies cells. The two gates have the same size, but their positions may differ. Activating a gate from inside the gate area starts a 3-second dimension transfer, and Jeongmin arrives at the same internal gate coordinate in the other dimension. Before activation, Jeongmin can move freely inside the gate area. After activation, Jeongmin cannot move until arrival. From the moment the gate is activated until the transfer completes, Jeongmin is in the passage between dimensions, so black holes do not affect Jeongmin, even if they cover the gate.

Find the minimum time for Jeongmin to reach the spaceship without being swept into a black hole. If a black hole reaches the spaceship's cell at the moment Jeongmin arrives there, Jeongmin is swept into it together with the spaceship.
Input
The first line contains the number of black holes , the height and width of Jeongmin's dimension, and the height and width of the spaceship's dimension. (; ; are even)
The second line contains the height and width of the dimension-travel gate. (; )
The third line contains the coordinates and , which are the position of the gate's internal origin in Jeongmin's dimension. (; )
The fourth line contains the coordinates and , which are the position of the gate's internal origin in the spaceship's dimension. (; )
The next lines each give one black hole. Each line contains the dimension number , the row , and the column , separated by spaces. is 1 if the black hole is in Jeongmin's dimension and 2 if it is in the spaceship's dimension. and are always within the dimension's bounds.
All input values are integers.
Output
Print the minimum time, as an integer, for the spaceship to be reached. If the spaceship cannot be reached without being swept into a black hole, print hing....