<p><span class="h-card" translate="no"><a href="https://mastodon.social/@regehr" class="u-url mention">@<span>regehr</span></a></span> if you have a solution for part 1, you ought to basically be there. You can either continue your search until you identify all paths with equal cost, and then backtrack through your data structures to find the cells (this can be efficient because the adjacencies are trivial).</p><p>Alternatively you can do the dumb thing I did because I was lazy and just attach a set of all the squares in the history to the state in your data structures, and take their union when you find another path to the same state.</p><p>I haven't coded up option 1 because who has time for that, but it should be 50-100x faster on the inputs in question.</p>