The youngest brother
Statement
Four brothers live in a house of size N×N. The youngest likes his older brothers, so when he wakes up in the morning he greets the first, the second and the third brother in that order. At the start all four brothers stand on different cells.
More precisely, the youngest starts from the cell he is on, visits the cells of the first, the second and the third brother in that order, then returns to his original cell. Each second he can move from his current cell to an adjacent cell up, down, left or right, and while he is moving several people may stand on the same cell. Also, to greet a given brother he has to reach the same grid cell that brother is on.
Write a program that computes the minimum time the youngest needs to reach every brother in order, greet them, and return to his original cell.
For example, look at the case N=6. In the grid a value of 0 means an empty cell with nothing on it, 1 is the cell of the first brother, 2 is the cell of the second, 3 is the cell of the third, and 4 is the cell of the youngest.
| 0 | 0 | 0 | 0 | 0 | 0 |
|---|---|---|---|---|---|
| 0 | 4 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 2 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 1 | 0 |
| 0 | 3 | 0 | 0 | 0 | 0 |
Write the position of each cell as (row, column), and assume rows and columns are numbered from 0. In this situation the best route for the youngest is (1, 1) → (4, 4) → (2, 3) → (5, 1) → (1, 1), which takes 18 (6 + 3 + 5 + 4) seconds in total. So the answer for this example is 18.
Input and Output Examples
Example 1
Input
6, [[0, 0, 0, 0, 0, 0], [0, 4, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0], [0, 3, 0, 0, 0, 0]]
Output
18
Example 2
Input
4, [[0, 1, 0, 0], [0, 2, 0, 0], [0, 0, 0, 0], [0, 4, 0, 3]]
Output
10
Input
First the size of the house N is given. N is a natural number from 3 to 100.
Next an array house holding the layout of the house is given, as a 2D array whose rows and columns both have length N. In the grid a value of 0 means an empty cell with nothing on it, 1 is the cell of the first brother, 2 is the cell of the second, 3 is the cell of the third, and 4 is the cell of the youngest.
Output
Return the minimum time the youngest needs to reach every brother in order, greet them, and return to his original cell.
Requirements
- You must write a solution(N, house) 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 5 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
5 public cases. Judging also uses hidden cases. You have to pass all 20 cases to solve this.
6, [[0, 0, 0, 0, 0, 0], [0, 4, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0], [0, 3, 0, 0, 0, 0]]
18
4, [[0, 1, 0, 0], [0, 2, 0, 0], [0, 0, 0, 0], [0, 4, 0, 3]]
10
10, [[0, 0, 0, 4, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 3, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 0, 0, 0]]
28
54, [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 0, 4, 0, 0, 0, 0, 0], [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
22
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 5 of practice test 1 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 5 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 11ms (excluded from the ranking)
Log in and you can see the code once you solve this problem.