Our hero, Link, finds himself in a forest most plain. Scattered around him, northways, eastways, westways and southways, are open plots of grass, impenetrable woods, and scattered, rare Heart Pieces. The question is: Can Link get all of them?
This link contains a zip file, with a single file in the Input Format specified for this collection of problems, of mazes.
Let be the sum of all indices (1-based) of mazes for which Link can access all Hearts within the maze, by moving up, down, left, or right through clearings and Heart Pieces, but not impenetrable forest. Find .
Example:
Input:
3
2 2
L#
#H
3 3
L..
##.
H..
6 6
H.....
.####H
.#.H#.
.#..#.
H####.
.....L
Output:
2
Output Explained:
In the first maze, there is one Heart Piece, but Link cannot access it - there are impenetrable trees in the way in both directions, and Link cannot move diagonally.
In the second maze, there is a single Heart Piece, and Link can access it by going right, right, down, down, left and left again, so we add its index, , to the total.
In the third maze, there are four Heart Pieces, and while Link can reach three of them, the fourth is blocked off in the middle, so we do not add this maze's index to the total.
Thus, only is added to the total, so the answer is .
Details:
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.
There are multiple ways to solve this problem, all equally valid. The idea at the core is Graph Traversal , where BFS and DFS are perhaps the two most commonly known ways of traversing a graph.
The condition for each index is "Link must be able to access all the Heart Pieces." One simple way of answering this question is first to ask "How many Heart Pieces are there?", followed by "How many Heart Pieces can Link reach?", finished by "Are these two numbers the same?" Equivalently, one could ask the more advanced question, "Are all of the Heart Pieces in the same connected component as Link?" We'll detail the solution to the first set of questions.
Click this link to view the scores for each maze from the input in order ( 1 if all hearts are reachable, 0 if not). 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!)