Touch mission
Statement
On an N×N board there are two or more target points. The square where the participant stands is marked "P". A wall is marked "X" and an empty square is marked "B". Each target point is marked "O". There is one participant, and in one second the participant can move one square up, down, left or right. The participant cannot move onto a wall square, but can move onto an empty square or a target point.
Once the touch mission starts, the participant clears it only by reaching the target point whose shortest distance is the largest among the points the participant can reach. In other words, the participant has to touch the farthest reachable point to clear the mission. Points that cannot be reached are excluded from the points to touch.
For example, assume the case N=6 below. Writing a position as (row, column), (1, 2) is the farthest target point in the current situation and clearing takes 7 seconds. Assume that row numbers and column numbers start at 1.
| B | O | B | B | B | B |
|---|---|---|---|---|---|
| X | X | X | X | X | B |
| B | B | O | B | P | B |
| X | X | B | X | B | B |
| B | B | O | X | B | B |
| B | B | B | B | B | B |
The participant aims to clear the mission in the least time. In this example the participant can clear in 7 seconds by taking the optimal path (3, 5) → (3, 6) → (2, 6) → (1, 6) → (1, 5) → (1, 4) → (1, 3) → (1, 2).
However, the participant may use one of two skills beforehand. The touch mission itself starts after that. The two skills are described in detail below.
① Remove one target: you may pick one target point and delete it. ② Remove one wall: you may delete one wall. That is, you turn one wall X into a B.
In other words, before the touch mission runs, the participant can use one of the two skills and so cut the time needed to clear it. First consider the case below, where the "② Remove one wall" skill deletes one wall. The position of the deleted wall was shaded. Here you can see the answer drop to 5. In this example, deleting a wall at any position other than (2, 5) does not make the distance to the farthest point (1, 2) smaller than 5.
| B | O | B | B | B | B |
|---|---|---|---|---|---|
| X | X | X | X | B | B |
| B | B | O | B | P | B |
| X | X | B | X | B | B |
| B | B | O | X | B | B |
| B | B | B | B | B | B |
Or suppose the "① Remove one target" skill is used, as below, to delete the target point at (5, 3). When the touch mission then starts, the distance to the farthest target point is the distance to (1, 2), which is 7. So clearing takes 7 seconds.
| B | O | B | B | B | B |
|---|---|---|---|---|---|
| X | X | X | X | X | B |
| B | B | O | B | P | B |
| X | X | B | X | B | B |
| B | B | B | X | B | B |
| B | B | B | B | B | B |
On the other hand, suppose the "① Remove one target" skill is used, as below, to delete the target point at (1, 2). When the touch mission then starts, the distance to the farthest target point is the distance to (5, 3), which is 4. So clearing takes 4 seconds.
| B | B | B | B | B | B |
|---|---|---|---|---|---|
| X | X | X | X | X | B |
| B | B | O | B | P | B |
| X | X | B | X | B | B |
| B | B | O | X | B | B |
| B | B | B | B | B | B |
So the final answer in this example is 4.
Input and Output Examples
Example 1
Input
6, [["B", "O", "B", "B", "B", "B"], ["X", "X", "X", "X", "X", "B"], ["B", "B", "O", "B", "P", "B"], ["X", "X", "B", "X", "B", "B"], ["B", "B", "O", "X", "B", "B"], ["B", "B", "B", "B", "B", "B"]]
Output
4
Example 2
Input
6, [["P", "B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B", "B"], ["B", "B", "X", "X", "X", "X"], ["B", "B", "X", "X", "X", "X"], ["B", "B", "X", "X", "X", "O"], ["B", "B", "X", "X", "O", "O"]]
Output
-1
Example 3
Input
6, [["P", "B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B", "B"], ["B", "B", "X", "X", "X", "X"], ["B", "B", "X", "X", "X", "X"], ["B", "B", "X", "X", "X", "O"], ["B", "B", "X", "B", "O", "O"]]
Output
11
Input
First the board size N is given as a natural number. N is a natural number between 4 and 40.
Then an array board holding the information of the N×N board is given. There is exactly one square "P" where the participant stands, and there are at least 2 target points "O".
Output
Return the minimum time needed to clear the touch mission, given that the participant may use one of the two skills beforehand. If the mission cannot be cleared because no target point is reachable whatever you do, return -1.
Requirements
- You must write a solution(N, board) function and return the answer
- 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 20 cases, and 6 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 20 cases to solve this.
Examples
6 public cases. Judging also uses hidden cases. You have to pass all 20 cases to solve this.
6, [["B", "O", "B", "B", "B", "B"], ["X", "X", "X", "X", "X", "B"], ["B", "B", "O", "B", "P", "B"], ["X", "X", "B", "X", "B", "B"], ["B", "B", "O", "X", "B", "B"], ["B", "B", "B", "B", "B", "B"]]
4
6, [["P", "B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B", "B"], ["B", "B", "X", "X", "X", "X"], ["B", "B", "X", "X", "X", "X"], ["B", "B", "X", "X", "X", "O"], ["B", "B", "X", "X", "O", "O"]]
-1
6, [["P", "B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B", "B"], ["B", "B", "X", "X", "X", "X"], ["B", "B", "X", "X", "X", "X"], ["B", "B", "X", "X", "X", "O"], ["B", "B", "X", "B", "O", "O"]]
11
35, [["B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "O", "B", "X", "X", "B", "B", "B", "B", "B", "O", "X", "B", "B"], ["B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "X", "B", "B", "X", "B", "B", "B", "B", "B", "X", "X", "B", "X", "B", "B", "B", "X", "B", "B"], ["B", "B", "B", "X", "B", "B", "B", "B", "X", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "X", "B", "B", "B", "B", "B", "O", "B", "B", "B"], ["B", "B", "B", "B", "X", "B", "X", "B", "X", "B", "B", "B", "B", "B", "O", "B", "X", "B", "B", "B", "X", "B", "B", "B", "X", "B", "X", "X", "O", "B", "B", "B", "B", "O", "B"], ["B", "B", "B", "B", "B", "B", "X", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B", "B", "X", "B", "B", "O", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "X", "B", "X", "B", "B", "B", "O"], ["B", "X", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "O", "O", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B ... cut off here (6,199 chars in total)
36
22, [["B", "B", "O", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "O", "B", "B", "X", "B"], ["B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "O", "X", "B", "X", "B", "B", "B", "O", "B"], ["B", "X", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B"], ["B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "X", "B", "B", "B", "B"], ["B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "O", "X", "B", "B", "B", "B", "B", "B", "B", "B"], ["B", "B", "B", "X", "X", "O", "B", "B", "B", "X", "B", "X", "B", "B", "X", "B", "B", "X", "B", "B", "O", "O"], ["B", "B", "B", "B", "B", "O", "B", "B", "B", "B", "B", "B", "B", "B", "O", "O", "B", "O", "X", "B", "B", "B"], ["B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "X", "B", "B"], ["O", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"], ["X", "B", "B", "B", "B", "X", "B", "O", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "X", "B", "B", "X"], ["B", "X", "O", "B", "B", "B", "O", "X", "B", "B", "B", "O", "B", "B", "B", ... cut off here (2,468 chars in total)
23
22, [["B", "B", "B", "B", "X", "X", "X", "B", "B", "B", "X", "B", "X", "B", "B", "B", "B", "O", "X", "B", "B", "B"], ["O", "B", "B", "B", "B", "X", "B", "B", "B", "B", "O", "B", "B", "B", "B", "B", "B", "X", "X", "B", "B", "X"], ["X", "B", "B", "B", "X", "O", "B", "B", "B", "B", "B", "O", "O", "B", "B", "X", "B", "B", "B", "B", "B", "B"], ["B", "B", "X", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "O", "B", "B", "B", "B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "O", "B", "B", "B", "B", "B", "B", "B", "B", "B"], ["X", "B", "X", "B", "B", "O", "B", "B", "B", "X", "B", "B", "O", "B", "X", "B", "X", "B", "B", "B", "B", "X"], ["B", "B", "B", "O", "B", "B", "B", "O", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"], ["B", "B", "B", "B", "X", "B", "B", "X", "X", "X", "B", "B", "B", "B", "B", "B", "O", "B", "X", "B", "B", "B"], ["X", "B", "B", "B", "B", "B", "X", "X", "X", "B", "B", "B", "B", "O", "B", "B", "X", "B", "B", "B", "X", "B"], ["B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B"], ["B", "B", "B", "B", "B", "B", "B", "B", "B", "X", "B", "B", "B", "B", "B", ... cut off here (2,468 chars in total)
27
Notes
Background that helps with solving. The rules you have to follow are in the input and output specs and the requirements above.
This is problem 2 of practice test 3 in the original.
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 6 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.
Input read time 6ms (excluded from the ranking)
Log in and you can see the code once you solve this problem.