Batman Begins
Time limit2sMemory limit256 MB
Find the fastest drive from start to target on a blocked grid where the car speeds up and brakes at a fixed rate and must stop before each turn and at the goal.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph, Math
- Solved
- No attempts yet
Problem
Ra's Al Ghul heads the centuries-old League of Shadows and is an international terrorist. What he wants is a world in perfect environmental balance, and he believes the best way to reach that balance is to wipe out most of humanity.
As corruption spread through Gotham City, Ra's Al Ghul decided to destroy the city with a biological weapon. He plans to move a powerful microwave emitter to the main water hub of Gotham City by train and release a genetically engineered virus there. Batman and James Gordon decided to stop him, and Gordon drives the Batmobile to reach the water hub before the train does. Write a program that computes the minimum time Gordon needs to get there.
Gotham City is a grid of intersections. roads run east to west, roads run north to south, and two intersections next to each other on the same road are metres apart. Some intersections are closed, so the Batmobile can neither enter nor drive through them. Water surrounds the city on every side, so the Batmobile cannot leave the grid either.
The Batmobile works like this.
- It starts at rest at Gordon's intersection.
- Its acceleration and its deceleration always have magnitude m/s.
- It has a top speed, and it reaches that speed from rest in 5 seconds. The top speed is therefore m/s, and the Batmobile never drives faster.
- It drives along the roads only, and it has to come to a full stop before it turns left or right.
- It has to arrive at the water hub at speed zero.
Driving straight through an open intersection keeps the current speed. Every grid holds exactly one starting intersection and exactly one water hub, and the water hub is always reachable.
The equations of linear motion with constant acceleration are
Input
The first line holds the number of test cases ().
The first line of each test case holds four integers , , and (, , ): the number of horizontal roads, the number of vertical roads, the distance in metres between two adjacent intersections, and the acceleration of the Batmobile in m/s.
The next lines hold characters each. # is a closed intersection, G is Gordon's starting intersection, W is the water hub, and . is an open intersection.
Output
For each test case, print the minimum time in seconds needed to reach the water hub on its own line. Round the value at the third decimal place and print two decimals, rounding a value exactly halfway up. Keep a trailing zero in the second decimal place.
The answer is always further than from a rounding boundary, so double precision arithmetic is enough.