The king of the desert, and Link's arch nemesis, Ganondorf, has learned of the hero's rampant collecting of Heart Pieces, and that hardly aligns with his evil designs. Determined to stop the hero from progressing, the evil king has entered the Lost Woods himself, to collect Heart Pieces before Link can get them.
This link (1) contains a zip file, with a single file in the Input Format specified for this collection of problems, of 100 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 maximum number of Heart Pieces Link can collect despite Ganondorf's interference.
Find .
Example:
Input:
4
5 5
##.##
..G..
H###H
.###.
..L..
5 5
##L##
.....
H###H
.###.
..G..
5 5
##.##
..G..
H#.#H
.#.#.
..L..
7 4
##L.G##
H.#.#.H
#.....#
H.###.H
Output:
13
Output Explained:
In the first maze, Ganondorf can indefinitely mimic Link's movements left and right until he becomes adjacent to a Heart Piece, which he can then collect before Link can, and then go fetch the second one before Link as well. Though Link can prolong the opening indefinitely, he will never collect a single Heart Piece either way, so the score for this maze is .
In the second maze, Link and Ganondorf are in opposite positions as the first maze, with Link set back one space to compensate for first-turn advantage. Despite this, Link cannot employ the same strategy Ganondorf did - if he did, Ganondorf could simply sit still indefinitely, and Link would never collect a Heart Piece, which is Ganondorf's optimal end result. Link must therefore sacrifice one Heart Piece to Ganondorf by going after the other one, making the score for this maze times the index.
In the third maze, the situation is similar to the first maze, but now Link has a new avenue of approach. Though the path down the middle is longer towards each Heart Piece than the paths down the sides, by going up the middle Link forces Ganondorf to choose a single Heart Piece to defend, sacrificing the other to Link, making the score for this maze times the index.
In the fourth maze, Link is equidistant from all four Heart Pieces, same as Ganondorf, so with first turn advantage, he can get at least one. However, no matter which path Ganondorf takes, Link will also be able to get a second Heart Piece, but not a third, if he reacts properly to Ganondorf's movements. The score for this maze is thus times the index.
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.
As the examples show, great complexity emerges from simple rules. Any solution that correctly computes the answer to this problem must somehow take into account infinite strings of moves that represent 'stalemate' behavior, so any sort of brute-force approach is highly unlikely to work. A number of heuristic approaches present themselves at first glance, but again, as the examples show, these seem unlikely to produce optimal results in all cases.
In order to determine optimal behavior on Ganondorf's part, and therefore the maximum score Link can achieve, we need to be able to consider all possible behaviors in a comprehensive manner. We'll do this by composing a directed graph modeling all potential game states. What makes a game state unique?:
Multiplying the cardinality of all of these pieces of information together, and declaring N to be the number of Heart Pieces within a maze, we find that there are O ( 2 ⋅ ( W H ) ⋅ ( W H ) ⋅ ( 2 N ) ⋅ N ) = O ( W 2 H 2 N 2 N ) possible game states approximately speaking. Given the problem constraints, this amounts to about 300k states in the worst case, so any algorithm we attempt should be close to linear if it is to succeed in reasonable time!
Computing the game state graph is doable in linear time via BFS , by starting with the initial state, and using computation of all possible subsequent states due to any of Link's / Ganondorf's available moves, properly reducing the set of Heart Pieces, and/or incrementing Link's score when one is collected. Any state which has no Heart Pieces remaining is a game-over state, so we can stop searching from those. Once we have this game state graph, which is directed , and bipartite , but definitely not acyclic , we're ready to determine optimality.
To solve the problem, we will ask two generic questions about the game state graph, show that they can be used to recursively answer each other in finitely many iterations, and then show that these questions allow us to compute Link's maximum score. The algorithm presented is very similar to the minimax algorithm used in game theory. We can't use it directly because our game has more than 2 possible outcomes, and because the game state graph has cycles, and therefore cannot be modeled as a tree.
The first question is, "Given a set of candidate states one may traverse, including a start node, is it possible for Link to force the game to reach any one of a set of arbitrary target states?", and the second question is, "Given a set of candidate states one may traverse, including a start node, can Ganondorf prevent the game from ever reaching any one of a set of arbitrary target states?"
Now consider the first question. Let's consider the set of candidate nodes which have at least one out-edge pointing to one of the targeted states, and of course only candidate nodes for which it is Link's turn. If Link is to reach any of the targeted states, Link must force traversal to one of these nodes. If the start node is contained within the set, the answer is 'Yes' and we're done - likewise, if the set is empty, the answer is 'No', and we're done. Otherwise, we consider the complement problem: Of all of the nodes in this set, can Ganondorf indefinitely avoid traversal to any of them? If he can, then Link cannot force traversal to any of the target nodes, but if he can't, then Link certainly can.
Let's consider the second question! We consider the set of candidate nodes for which all out-edges point to one of the targeted states, and only candidate nodes for which it is Ganondorf's turn. If Ganondorf is to avoid all of the targeted states, then Link must not be able to force traversal to any of the nodes in this set, for if he does, Ganondorf will be forced to make a move that lets Link reach the target nodes. Similar logic follows. If the set is empty, the answer is 'Yes', and we're done. Otherwise, consider the compliment: Of all the nodes in this set, can Link force traversal to any of them? If he can, then Ganondorf cannot avoid the target nodes, but if he can't, then Ganondorf can.
This leads to the following pseudo-code:
In this way, from a set of target_states, nodes are examined in reverse order up the graph, and discarded from the candidate set as they are determined to be either a 'win' or 'lose' node for Link/Ganondorf. Since every method call must discard at least one candidate state to recurse, and, by using the inverted game state graph for efficiency, the in-edges of every state need only be considered once, the algorithm can be executed in linear time with the right data structures, since each game state is capped at 5 out-going, and in-coming edges.
Once we have these functions, it is trivial to determine Link's maximum score. For each score Link might obtain, simply ask:
And return the maximum score for which the above function call returns True.
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!)