DWITE Online Computer Programming Contest

Mountain Hiking

January 2011
Problem 4

Mountain hiking is a very adventurous yet somewhat dangerous pastime. On certain mountain ranges, the heights could vary sharply. An amateur hiker can move to an adjacent (left/right, up/down, but not diagonally) location only if the height difference with the current location is at most 1. Given the height map of a mountain range, determine the distance of the shortest viable path between the left and right edges.

The input file DATA4.txt will contain 5 test cases. Each test case consists of a 10×10 map of the digits 0 to 9, with each digit representing the height at that location. A line of hyphens ---------- follows each test case for visual separation.

The output file OUT4.txt will contain 5 lines, each being the least number of steps to cross the mountain range in that case. If the hiker can’t get across, output “IMPOSSIBLE” (without quotes) instead.

Notes: The hiker could start at any of the leftmost positions. The steps counted are the transitions from one location to the next. Thus appearing in that very first location requires no steps.

Sample Input (only 1st case shown):
9324892342
1334343293
3524523454
2634232043
0343259235
2454502352
4563589024
7354354256
9343221234
2653560343
----------
Sample Output (only 1st case shown):
11

Explanation for the sample test case:

9324892342
1334343293
3524523454
2634232043
0343259235
2454502352
4563589024
7354354256
9343221234
2653560343