God Game
Time limit1sMemory limit512 MB
Find the minimum time for a ball on an N by M grid to reach the goal while dodging obstacles that loop clockwise around small squares forever.
- Level
Hard8 of 10
- Topics
- BFS, Graph, Simulation, Math
- Solved
- No attempts yet
Problem
Taeyoung is addicted to games. He fell for a game called Legend of League, stopped studying, and filled his days with it. Junhyung, who found that pathetic, decided to build a god game himself and pull Taeyoung out of Legend of League.
Here is the game Junhyung built. The board is an rectangle that lies in the first quadrant of the coordinate plane with one vertex at the origin. Both and are integers. The goal is to move a ball that starts at at time to . All of these are integer coordinates. At each integer time , Taeyoung does one of the following two things.
- Leave the ball where it is for 1 second.
- If the ball is at , move it for 1 second at a speed of 1 cell per second to one of , , , . The destination must be a position where the ball is allowed to be.
At every moment the ball's coordinates must satisfy and . Even among the coordinates inside that range there can be positions where the ball is not allowed to be. Note that at any time , at least one of the ball's coordinate and coordinate is an integer.
That alone would make the game too easy, so Junhyung put moving obstacles into some of the stages. Obstacle starts at the integer coordinate at and travels clockwise around a square whose side is the integer () at a constant speed of 1 cell per second. Here is the lower left vertex of that square. For example, if the obstacle follows the path
with a period of 8 seconds. Every point on an obstacle's path stays inside the board, but the path may run through cells where the ball is not allowed, and two obstacles may share a path. If the ball and an obstacle are at the same coordinate at some time , the game is over. Here does not have to be an integer, and the point where the ball meets the obstacle does not have to be an integer coordinate.
Junhyung finished the game and handed it to Taeyoung, calling it a god game. The game was very hard, and Taeyoung played his heart out but got stuck on the third stage. We cannot play in his place, but for poor Taeyoung we can at least work out the minimum time it takes to clear a given stage.
Spacing out at the very start and losing, or getting hit by an obstacle the moment the ball reaches the destination, would be too unfair, so the merciful Junhyung never built such a case. In other words, the paths of the obstacles pass through neither the start nor the destination.
Input
The first line contains the board size and (). The next lines describe the board. If the -th character of the -th line is ., the ball is allowed to be at ; if it is #, the ball is not allowed to be at . S is the start and E is the destination. The next line contains the number of obstacles (). Each of the next lines contains the integers , , (, , ) describing one obstacle.
Output
Print the minimum time Taeyoung needs to clear the game. If he can never clear it, print INF.
Hint
On the board given in the input format, the downward direction is and the rightward direction is . There may also be no obstacles at all ().

The figure above shows a board with four obstacles of side 1 standing in a row. No matter how well the ball is moved, it cannot get past the stretch covered by those four obstacles. In that case there is no way to clear the game. With only three obstacles in the same spot, the right timing takes the ball through that stretch and clears the game.