The staff fighter
Statement
One staff fighter has to fight many grapplers on an N×N board. On the board empty space is marked "B" and a grappler's position is marked "G", and no two people can stand in the same position. At least 2 grapplers are given, and the grapplers start on different cells. At least 1 empty cell with no grappler on it is given.
Once the staff fighter picks a starting position among the empty cells, the grapplers begin to move. Each grappler can move up, down, left or right from its current position every second, and no two grapplers can stand in the same position while they move. The staff fighter wants to get as far away from the grapplers as possible before the fight starts.
So, to decide the staff fighter's starting position, write a program that finds the empty cell whose distance to the nearest grappler is the largest. If several empty cells have the largest distance to the nearest grappler, return the position with the lowest (smallest) row number. If several positions have the lowest row number, return the position with the lowest column number.
For example, look at a case with N=5. If each position is written as [row, column], with row and column indices starting at 0, the 3 grapplers in the example below stand at [1, 1], [2, 3] and [4, 2], in that order.
| B | B | B | B | B |
|---|---|---|---|---|
| B | G | B | B | B |
| B | B | B | G | B |
| B | B | B | B | B |
| B | B | G | B | B |
In this case the points whose distance to the nearest grappler is the largest are shaded below. So in this example the largest distance to the nearest grappler turns out to be 3.
| B | B | B | B | B |
|---|---|---|---|---|
| B | G | B | B | B |
| B | B | B | G | B |
| B | B | B | B | B |
| B | B | G | B | B |
So, in the current example, the [lowest row, lowest column] position this problem asks for is [0, 4].
Input and output examples
Example 1
Input
5, [["B", "B", "B", "B", "B"], ["B", "G", "B", "B", "B"], ["B", "B", "B", "G", "B"], ["B", "B", "B", "B", "B"], ["B", "B", "G", "B", "B"]]
Output
[0, 4]
Example 2
Input
5, [["B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B"], ["B", "B", "B", "B", "G"], ["B", "B", "B", "G", "B"], ["B", "B", "G", "B", "B"]]
Output
[0, 0]
Input
First the board size N is given. N is a natural number from 5 to 500.
Next an array board is given, holding the board information as a two-dimensional array whose rows and columns are each N long. Empty space is marked "B" and a grappler's position is marked "G". No two people are ever in the same position. At least 2 grapplers are given, and at least 1 empty cell is given.
Output
Return the position of the empty cell whose distance to the nearest grappler is the largest, as a one-dimensional array in the form [lowest row, lowest column]. If several empty cells have the largest distance to the nearest grappler, return the position with the lowest row number. If several positions have the lowest row number, return the position with the lowest column number.
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 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.
5, [["B", "B", "B", "B", "B"], ["B", "G", "B", "B", "B"], ["B", "B", "B", "G", "B"], ["B", "B", "B", "B", "B"], ["B", "B", "G", "B", "B"]]
[0, 4]
5, [["B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B"], ["B", "B", "B", "B", "G"], ["B", "B", "B", "G", "B"], ["B", "B", "G", "B", "B"]]
[0, 0]
68, [["B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "G", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"], ["B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B" ... cut off here (23,260 chars in total)
[0, 9]
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 6 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 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.
Input read time 56ms (excluded from the ranking)
Log in and you can see the code once you solve this problem.