This page is still under construction.

Parts of this page are still being built. What you see may change.

Batman Begins

Time limit2sMemory limit256 MB

Summary
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. HH roads run east to west, VV roads run north to south, and two intersections next to each other on the same road are DD 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.

  1. It starts at rest at Gordon's intersection.
  2. Its acceleration and its deceleration always have magnitude aa m/s2^2.
  3. It has a top speed, and it reaches that speed from rest in 5 seconds. The top speed is therefore 5a5a m/s, and the Batmobile never drives faster.
  4. It drives along the roads only, and it has to come to a full stop before it turns left or right.
  5. 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

vf=vi+atv_f = v_i + a t

vf2=vi2+2a(xf−xi)v_f^2 = v_i^2 + 2 a (x_f - x_i)

xf=xi+vit+12at2x_f = x_i + v_i t + \frac{1}{2} a t^2

Input

The first line holds the number of test cases TT (1≤T≤1001 \le T \le 100).

The first line of each test case holds four integers HH, VV, DD and aa (1≤H,V≤5001 \le H, V \le 500, 1≤D≤10001 \le D \le 1000, 1≤a≤10001 \le a \le 1000): 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/s2^2.

The next HH lines hold VV 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 10−610^{-6} from a rounding boundary, so double precision arithmetic is enough.

Examples2

  1. Example 1

    Input
    3
    1 5 1 1
    G...W
    1 5 10 1
    G...W
    6 5 10 1
    G....
    ####.
    ...#.
    .#W#.
    .###.
    .....
    
    Expected output
    4.00
    13.00
    67.27
    
  2. Example 2

    Input
    6
    1 2 25 1
    GW
    1 2 1 1000
    GW
    1 2 1000 1
    GW
    2 1 4 1
    G
    W
    2 1 24 1
    G
    W
    2 1 26 1
    G
    W
    
    Expected output
    10.00
    0.06
    205.00
    4.00
    9.80
    10.20