Intro
Help! I'm stuck on a snow-covered mountain and I need to get down as fast as possible, preferably without dying. I have a map showing how high each part of the mountain is above the normal ground.
I am a novice skier, which means I like to stick to slopes that aren't too steep. I'm willing to go down just two moderately steep sections, however - but not back to back!
Rules
Your program's job is to, given a n x m matrix of height values, navigate from the skier's current position (the top left corner) to the base (the bottom right corner). Height values will be given as integers between 0 and 25, with 25 being the highest possible point and 0 being the base of the mountain. It should be noted the mountain does not have to start at 25 units high, but will always finish at the 0 in the bottom right corner.
There are a few constraints:
- The skier can move up, down, left, or right on the map. No diagonals.
- The skier is usually only comfortable on slopes that bring him down 2 units or less.
- The only exception to the above rule is that the skier can go down a maximum of two sections that will bring him down 3 or 4 units.
- The skier can move upwards if necessary, but only one unit at a time. The skier also cannot go upwards or flat then immediately thereafter go down a steep slope of 3 or 4 units, he must already be headed downwards before taking a steep slope. Also, his first move cannot be to go down a steep slope, and he cannot do two steep slopes back to back.
- It is possible for the skier to visit a location twice (as seen in the fourth example - this is due to the fact that the skier cannot go down two steep slopes in a row)
Input
As mentioned above, the map will be given by a matrix containing integer values from 0 to 25, with a 0 in the bottom left corner (there could be zeroes in other places on the map). These maps will always have at lease one best solution but could have several worse solutions. These matrices will always have spaces and line breaks as separators - double spaces will be used if the integer is just one character (see examples below)
Output
Your program is to output a sequence of moves (the format of this sequence does not matter so long as it is coherent) that shows the best way down the mountain - meaning fewest moves on the map. These moves can be represented by any strings, numbers, or characters you like, so long as they are distinct and are described in your solution. A sample output might look like RRDDRRRURRDDDDRR
, where R
means right, D
means down, U
means up, and L
means left.
Test cases
Input 1:
10 13 13 13 13
8 9 11 13 25
0 7 11 12 23
6 5 2 2 0
Sample output 1:
DRDDRRR
Input 2:
16 12 12 10 8 6
14 12 6 8 0 4
10 20 4 2 1 2
8 20 10 8 4 0
9 8 9 25 2 0
Sample output 2:
DDDDRRURRDR
or DRURRRRDDDD
Input 3:
20 19 15 11 0 9 15 6
18 20 13 12 11 9 18 12
16 25 9 7 6 2 1 1
12 20 10 4 4 4 3 2
13 12 13 0 0 0 0 0
Sample output 3:
RRDDRRDRRRD
Input 4:
20 18 10 11 12 0
19 11 1 4 5 0
18 16 3 3 3 3
19 8 7 17 19 3
18 20 11 12 13 3
17 17 16 16 14 2
20 21 11 14 11 0
Sample output 4:
DDDDDRRRRULLULRURRRDDDD
Scoring:
This is code-golf. Shortest answer wins - get creative!
U
s andL
s? Now it seems the skier will never move "away" from the goal. \$\endgroup\$