The traveler's climb
Statement
A mountain sits on an N×N board, and a traveler wants to climb it. The traveler starts at (1, 1), standing on the top left cell, and the goal is to end up at (N, N), the bottom right position.
Each position on the board is written as an integer between 0 and 100, which is the altitude (height) of that position. The altitude (height) of the starting position (1, 1) is always 0, and the goal is to reach the destination (N, N) spending as little stamina as possible.
The traveler can move from the current position to an adjacent position up, down, left or right. For example, assume N=4. If the traveler is at (1, 2), the board can be drawn as in the picture below. In the picture the traveler's position is shaded red, and the positions the traveler can move to are shaded blue.
The table above is a 4 row by 4 column grid. In the original, the traveler's position and the positions that can be reached are marked with shading only, so no cell values survived the text extraction.
As long as the traveler does not leave the mountain, moving from the current position to an adjacent position (up, down, left, right) is allowed. When one of the adjacent positions is chosen as the next position, the move is allowed even if the altitude differs. If the current position and the adjacent position have different heights (altitudes), the move costs as much stamina as the difference in altitude. If the altitudes are the same, no stamina is spent.
Write a program that works out the smallest amount of stamina the traveler needs to move from (1, 1) to (N, N).
Look at one example with N=5. The value written at each position on the board is the altitude (height) of that position. The altitude (height) of the starting position (1, 1) is always 0.
| 0 | 0 | 0 | 0 | 1 |
|---|---|---|---|---|
| 1 | 1 | 1 | 0 | 1 |
| 1 | 4 | 4 | 4 | 4 |
| 1 | 1 | 4 | 3 | 3 |
| 0 | 1 | 3 | 1 | 1 |
Assume each position means (row, column). In this example, moving in the order (1, 1) → (2, 1) → (3, 1) → (4, 1) → (4, 2) → (5, 2) → (5, 3) → (5, 4) → (5, 5) costs 5 stamina in total. In the picture, the shortest path from (1, 1) to (5, 5) is shaded, and the cells where the move costs stamina are colored blue.
| 0 | 0 | 0 | 0 | 1 |
|---|---|---|---|---|
| 1 | 1 | 1 | 0 | 1 |
| 1 | 4 | 4 | 4 | 4 |
| 1 | 1 | 4 | 3 | 3 |
| 0 | 1 | 3 | 1 | 1 |
Moving this way costs 5 stamina, and there is no way to spend less than that. (the smallest stamina cost) So the answer for this example is 5.
Input and Output Examples
Example 1
Input
5, [[0, 0, 0, 0, 1], [1, 1, 1, 0, 1], [1, 4, 4, 4, 4], [1, 1, 4, 3, 3], [0, 1, 3, 1, 1]]
Output
5
Example 2
Input
6, [[0, 5, 6, 2, 3, 8], [1, 1, 1, 1, 1, 1], [5, 6, 5, 3, 2, 3], [2, 6, 5, 2, 2, 4], [7, 7, 7, 3, 5, 6], [6, 7, 8, 4, 4, 3]]
Output
5
Input
First the size of the whole space N is given. N is a natural number less than or equal to 300.
Next an N×N array arr is given, holding the altitude (height) of each position of the mountain on the board. The altitude of each position is an integer between 0 and 100. The altitude (height) of the starting position (1, 1) is always 0.
Output
Return the smallest stamina cost for the traveler to move from (1, 1) to (N, N).
Requirements
- You must write a solution(N, arr) 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 2 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
2 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
5, [[0, 0, 0, 0, 1], [1, 1, 1, 0, 1], [1, 4, 4, 4, 4], [1, 1, 4, 3, 3], [0, 1, 3, 1, 1]]
5
6, [[0, 5, 6, 2, 3, 8], [1, 1, 1, 1, 1, 1], [5, 6, 5, 3, 2, 3], [2, 6, 5, 2, 2, 4], [7, 7, 7, 3, 5, 6], [6, 7, 8, 4, 4, 3]]
5
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 8 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 2 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 113ms (excluded from the ranking)
Log in and you can see the code once you solve this problem.