Escape Route

Interview

Time limit1sMemory limit128 MB

Summary
For each grid, find the ship and the planet nearest to it, breaking ties by row-major order, and print both coordinates with the distance to two decimals.
Level

Easy3 of 10

Topics
Array, Implementation, Math, Brute force
Solved
No attempts yet

Problem

You are surrounded by Imperial warships and must land on a planet to escape. Your best chance is to reach the planet closest to your current location. Unfortunately the navigation system has been damaged, so you must write a program that finds the planet closest to your ship.

Space is represented by an m×mm \times m grid made up of the following four characters:

  • s: your ship
  • w: an Imperial warship
  • p: a planet
  • -: open space

The location of every object (your ship, a warship, or a planet) is its position in the grid, written as (row, column) with both indices starting at 00; the top-left cell is (0,0)(0, 0). The table below shows the coordinates of every cell of a 4×44 \times 4 grid.

The distance between two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is

dist=(x1−x2)2+(y1−y2)2dist = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}

Input

The first line contains a positive integer nn, the number of data sets that follow. Each data set begins with a line containing a positive integer mm, followed by mm lines of exactly mm characters each; every character is one of s, w, p, or -. Each data set contains exactly one ship s and at least one planet p.

Output

For each data set print one line: the coordinates of your current location, a colon (:), the coordinates of the closest planet, another colon (:), and the distance to that planet rounded to two decimal places. Coordinates are printed as (row,column) with no spaces.

If several planets share the smallest distance, print the one that comes first in row-major order — the smallest row index, and among those the smallest column index.

Examples2

  1. Example 1

    Input
    2
    4
    -wp-
    s--w
    -w-p
    -pw-
    5
    s--wp
    w--pw
    -w-p-
    p--wp
    w--pw
    
    Expected output
    (1,0):(0,2):2.24
    (0,0):(3,0):3.00
    
  2. Example 2

    Input
    1
    2
    sp
    --
    
    Expected output
    (0,0):(0,1):1.00