Touring the observation units
Statement
A research institute has set up observation units all over a mountain area. The area is divided into an N × N grid, and each cell has its elevation written on it.
The grid uses the following three marks.
(1) P is the cell with the drone hangar. There is exactly one in the whole grid. (2) K is a cell with an observation unit whose battery has to be changed. There is at least one. (3) A dot (.) is an empty cell with nothing on it.
The drone has to start at the hangar, pass over every observation unit cell at least once, and then come back to the hangar. From its current cell the drone can move in one step to any of the eight cells that touch it horizontally, vertically or diagonally. It may pass over the same cell several times.
This drone breaks down when the air pressure changes too fast. So the gap between the highest elevation and the lowest elevation among the cells it passes should be as small as possible. The elevation of the hangar cell, where it starts, counts as a cell it passes.
Write a program that takes the marks of the area and the elevation of each cell, and computes the smallest gap between the highest elevation and the lowest elevation passed, over all routes that start at the hangar, visit every observation unit and come back to the hangar.
For example, suppose the marks of an area with N=4 are given by the table below.
| Row | Column 1 | Column 2 | Column 3 | Column 4 |
|---|---|---|---|---|
| Row 1 | . | . | . | K |
| Row 2 | K | P | . | . |
| Row 3 | . | . | . | . |
| Row 4 | . | K | . | . |
Suppose also that the elevation of each cell is given by the table below.
| Row | Column 1 | Column 2 | Column 3 | Column 4 |
|---|---|---|---|---|
| Row 1 | 45 | 50 | 2 | 35 |
| Row 2 | 25 | 20 | 2 | 29 |
| Row 3 | 2 | 47 | 17 | 41 |
| Row 4 | 13 | 30 | 22 | 38 |
The hangar is at row 2, column 2, and its elevation is 20. There are three observation units, at row 1 column 4 with elevation 35, at row 2 column 1 with elevation 25, and at row 4 column 2 with elevation 30.
Suppose the drone steps only on cells whose elevation runs from 17 to 35. Marking the cells it can step on with ○ gives the table below.
| Row | Column 1 | Column 2 | Column 3 | Column 4 |
|---|---|---|---|---|
| Row 1 | ○ | |||
| Row 2 | ○ | ○ | ○ | |
| Row 3 | ○ | |||
| Row 4 | ○ | ○ |
Starting from the hangar at row 2 column 2, the drone can reach row 2 column 1 and row 3 column 3, and from row 3 column 3 it can reach row 4 column 2, row 4 column 3 and row 2 column 4. From row 2 column 4 it reaches row 1 column 4. So it can visit all three observation units and then come back to the hangar. The elevation gap here is 35 - 17 = 18.
Any elevation range narrower than 18 leaves at least one of the three observation units out of reach. So the answer for this case is 18.
Input and Output Examples
Example 1
Input
4 ...K KP.. .... .K.. 45 50 2 35 25 20 2 29 2 47 17 41 13 30 22 38
Output
18
Example 2
Input
2 P. K. 5 1 38 42
Output
33
Example 3
Input
5 KKKKK KKKKK KKKPK KKKKK KKKKK 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Output
0
Input
The first line holds the side length of the area N. N is a natural number from 2 to 50.
The next N lines hold the marks of the area. Each line is N characters run together with no spaces, and each character is one of P, K and a dot (.). There is exactly one P in the whole grid, and there is at least one K.
The next N lines hold the elevation of each cell. Each line holds N numbers separated by spaces, and each elevation is a natural number from 1 to 1,000,000.
The whole input is at most about 20KB.
Output
Print on the first line the smallest gap between the highest elevation and the lowest elevation passed, over all routes that start at the hangar, visit every observation unit and come back to the hangar.
Requirements
- The drone can move to any of the eight cells that touch it. Diagonal moves count as well as horizontal and vertical ones
- The elevation of the hangar cell counts as a cell the drone passes
- The drone may pass over the same cell several times
- The grid has no walls, so a route that visits every observation unit and comes back to the hangar always exists
- If every cell the drone passes has the same elevation, the answer is 0
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
2 P. K. 5 1 38 42
33
4 ...K KP.. .... .K.. 45 50 2 35 25 20 2 29 2 47 17 41 13 30 22 38
18
5 KKKKK KKKKK KKKPK KKKKK KKKKK 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.