The forest is getting thicker, darker, and more dangerous. Heart Pieces are getting rarer, and open ground is no longer as abundant. Our hero now faces thick, but removable Trees in addition to the impenetrable ones, and while they are manageable, clearing Trees requires a fair bit of magic and work, so Link would prefer to cut as few Trees as possible.
This link (1) contains a zip file, with a single file in the Input Format specified for this collection of problems, of 1000 mazes.
Let be the sum of the score for each maze. We define the score to be the index of the maze (1-based) multiplied by the minimum number of Trees Link must cut in order to reach the singular Heart Piece in the maze. If Link can reach the Heart Piece without cutting any Trees, the score is zero for that maze. Link will always be able to reach the Heart Piece somehow, however.
Find .
Example:
Input:
3
3 3
L..
T#.
H..
5 5
L....
T###.
T###T
H###.
.....
10 1
LTTTTTTTTH
Output:
26
Output Explained:
In the first maze, the Heart Piece is very close by if Link cuts the Tree, but if he takes the long way, he doesn't need to cut any, so the score is .
Likewise, in the second maze, the Heart Piece is very close by if Link cuts the two Trees below, but if he takes the long way, he only needs to cut one Tree, so the score is .
In the third maze, Link has only one option, to cut the 8 trees in between him and the Heart Piece, so the score is .
Details and Assumptions :
in all mazes.
(1) - This link currently points to my Dropbox account, since there is currently no way to upload non-image content to Brilliant. Admin, feel free to delete this footnote if this is changed.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Plain old BFS and DFS are no longer sufficient to solve Link's problems. Now, we need to consider the concept of shortest path with unequal edge weights. There are many algorithms that do this, though some of them are a mite expensive for our graph sizes, such as A* , and Bellman Ford . In this solution, we'll go over an implementation of Dijkstra's Algorithm , which can be tuned to solve all mazes in this puzzle in O ( W ⋅ H ⋅ lo g ( W ⋅ H ) ) time.
An important note to consider going forward is that all Graphs which model the individual Lost Woods Mazes thus far are planar , have linearly as many edges as they do nodes, and as a result, have a constant, maximum vertex degree . These limits are not important now, but in future problems, they will make problems that are intractable on general graphs, tractable.
Dijkstra's Algorithm is an algorithm that, given a source node, can compute the minimum distance, or shortest path, to all other reachable nodes in the graph. The way it does this is fairly straight forward:
1) Assign a distance value to every node, with 0 at the source, and infinity at all other nodes, to start.
2) Make a list of tentative nodes, starting with all nodes adjacent to the source. Set the tentative distances of these nodes to be equal to the edge weights linking them to the source.
3) Choose the tentative node closest to the source (or select one arbitrarily if there's a tie), and mark its tentative distance as final.
4) Using that final distance, reach out to any adjacent nodes to add them to the list of tentative nodes, and adjust their tentative distances if necessary. (I.e., if final distance + edge weight to adjacent node < tentative distance of adjacent node, change the tentative distance to the new low).
5) Go to step 3, and repeat, until either a) the intended destination is reached, or b) there are no more tentative nodes.
The scale of the problem set, and the mazes within it, means that running Dijkstra with a naive, plain list for the tentative nodes will run in tractable time, at O ( W ⋅ H ⋅ ( W + H ) ) , due to the graph being planar. However, one can use a min-heap for the tentative node list instead, and this reduces the runtime per maze to O ( W ⋅ H ⋅ lo g ( W ⋅ H ) ) . It is even possible to get the runtime down to O ( W ⋅ H ) by taking advantage of the fact that all edge weights are in { 0 , 1 } .
To apply the algorithm to this problem, simply make the following transformations:
Click this link to view the scores for each maze from the input in order, sans multiplication by the index. If you were not able to get the right answer, consulting these individual results may help you find the problem (or, if I got something wrong, the bug in my own solution!)