You are a railroad entrepreneur in the 19th-century United States when trains become popular because they are the most efficient means of transporting large volumes of materials by land. There is a national need for railroad tracks from the east coast through some recently colonized lands in the west.
To accommodate this need, the U.S. government is going to levy a tax to subsidize railroads. They have promised to pay money to your railroad company for each mile of track laid. Since laying tracks in hilly and mountainous regions is more expensive than laying tracks in flat land, they adjust the amount they give accordingly. That is, the government will pay
- $5,000 per mile of track laid in flat land
- $12,500 per mile of track laid in hilly land
- $20,000 per mile of track laid in mountains.
Of course, this plan does not accurately reflect how much it actually costs to lay tracks.
You have hired some cartographers to draw relief maps of the regions where you will be laying track to analyze the elevation. Here is one such map:
S12321 121234 348E96
Each digit represents one square mile of land.
S is the starting point,
E is the ending point. Each number represents the intensity of the elevation changes in that region.
- Land numbered 1-3 constitutes flat land.
- Land numbered 4-6 constitutes hilly land.
- Land numbered 7-9 constitutes a mountain range.
You have, through years of experience building railroad tracks, assessed that the cost of track building (in dollars) satisfies this formula:
Cost_Per_Mile = 5000 + (1500 * (Elevation_Rating - 1))
That means building on certain elevation gradients will cost you more money than the government gives, sometimes it will be profitable, and sometimes you will just break even.
For example, a mile of track on an elevation gradient of 3 costs $8,000 to build, but you only get paid $5,000 for it, so you lose $3000. In contrast, building a mile of track on an elevation gradient of 7 costs $14,000, but you get paid $20,000 for it: a $6,000 profit!
Here is an example map, as well as two different possible paths.
S29 S#9 S## 134 1#4 1## 28E 2#E 2#E
The first track costs $30,000 dollars to construct, but the government pays you $30,000 for it. You make no profit from this track.
On the other hand, the second costs $56,500 to build, but you get paid $62,500 for it. You profit $6,000 from this track.
Your goal: given a relief map, find the most profitable (or perhaps merely the least expensive) path from the start to the end. If multiple paths tie, any one of them is an acceptable solution.
You are given text input separated with a rectangular map of numbers and one start and end point. Each number will be an integer inclusively between 1 and 9. Other than that, the input may be provided however you like, within reason.
The output should be in the same format as the input, with the numbers where track has been built replaced by a hash (
#). Because of arbitrary regulations imposed by some capricious politicians, tracks can only go in horizontal or vertical paths. In other words, you can't backtrack or go diagonally.
The program should be able to solve in a reasonable amount of time (i.e. <10 minutes) for maps up to 6 rows and 6 columns.
This is a code golf challenge, so the shortest program wins.
I have an example (non-golfed) implementation.
S12321 121234 348E96 S12321 ###### 3##E## S73891 121234 348453 231654 97856E S#3### 1###3# 3##### ###### #####E