Swamp Kangaroo

Time limit1sMemory limit128 MB

Summary
On a small grid of land and water, find the shortest time for a kangaroo to hop from K to G, where hop cost depends on the previous jump distance and direction.
Level

Medium6 of 10

Topics
Graph, Shortest path, Implementation, Simulation
Solved
No attempts yet

Problem

Kangaroos are fascinating creatures. For one, carrying their offspring in a pouch is very cute, and it reminds us of our own ways of transporting babies. For another, they can jump really far. That is quite useful — in particular if you are a kangaroo stuck in a swamp with only small islands of land and crocodiles swimming around, since you would rather not land in the water. Besides raw jumping strength, it also helps to have some computational power to work out exactly how to use that strength to get where you need to go. That is where your programming friends come in.

We model kangaroo movement as follows. A kangaroo can only move North–South or East–West; no other directions (such as diagonals) are allowed. In a single hop the kangaroo can jump any integer distance between 11 and 55, and each hop takes one unit of time. After a longer jump, however, it must rest before jumping again: after jumping a distance dd, the kangaroo has to rest (d−1)2(d-1)^2 units of time before it can jump again. In addition, if the next jump is in a different direction from the previous one, the kangaroo needs one extra unit of time between the two jumps to turn around.

The swamp is described by a two-dimensional grid. Each cell is either water, written as a dot ., or land, written as X. Two cells are marked specially: K is the kangaroo's starting position and G is the goal it wants to reach (both are of course land). A jump may pass over water, but the kangaroo must land on a land cell that lies inside the grid. Find the shortest time in which the kangaroo can reach the goal, if it can reach it at all. It does not matter whether the kangaroo is tired when it arrives; it does not have to rest upon arrival.

Input

The first line contains the number of data sets KK. Each of the following data sets has this form: the first line contains two integers hh and ww (1≤h,w≤301 \le h, w \le 30), the height and width of the swamp map. The next hh lines each contain exactly ww characters, where every character is ., X, K, or G. Together these h×wh \times w characters describe the swamp. Each data set contains exactly one K and exactly one G.

Output

For each data set, print a line containing Data Set x:, where x is the data set's number (starting from 11). On the following line print the minimum amount of time in which the kangaroo can reach the goal, or Impossible if the goal cannot be reached. Print a blank line between consecutive data sets.

Examples3

  1. Example 1

    Input
    1
    12 30
    .......XXX......XX.....X.X.XX.
    XK....XXXXXXXX..XX............
    X......XXXXX....XX.X.X.X....X.
    .......XXX....XX..............
    ..............XX...........XX.
    ...........................XX.
    ...XX....XX................XX.
    ..XXXX.....................X..
    ..XXXX........................
    ...XX.........XX....X....XXX..
    ........XX....XX.X..X..X.XGX..
    .......XX................XXX..
    
    Expected output
    Data Set 1:
    64
    
  2. Example 2

    Input
    1
    1 2
    KG
    
    Expected output
    Data Set 1:
    1
    
  3. Example 3

    Input
    1
    1 6
    K....G
    
    Expected output
    Data Set 1:
    1