Escape Route
InterviewTime limit1sMemory limit128 MB
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 grid made up of the following four characters:
s: your shipw: an Imperial warshipp: 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 ; the top-left cell is . The table below shows the coordinates of every cell of a grid.

The distance between two points and is
Input
The first line contains a positive integer , the number of data sets that follow. Each data set begins with a line containing a positive integer , followed by lines of exactly 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.