r/AskComputerScience • u/RLutz • Mar 14 '21
Is this problem intractable?
So some background, there's a game called Loop Hero. We don't need to get too into the details of it, but there's an optimization problem in the game that basically works as follows:
Given an m x n region, place "thickets" and "rivers" to generate the highest possible score.
A thicket which has no rivers above, below, left, or right of it, is worth 2 points.
A thicket with 1 adjacency is worth 4 points.
A thicket with 2 adjacency is worth 8 points.
A thicket with 3 adjacency is worth 12 points.
A thicket with full (4) adjacency is worth 16 points.
A river must begin on the border of a region and it cannot branch, in other words, you can't ever backtrack on a river.
This app (which doesn't enforce the river placement rules) will give you a very intuitive feel for the problem: https://loopherolayout.xyz/#
I wrote an algorithm to attempt to solve this problem via brute force, but I believe the time-complexity of my solution is O(n!) and so solving the problem for even a 5x12 region seems intractable (I've been searching on 8 threads for 6+ hours and no threads have finished a full search from their starting point). The code can be found here: https://github.com/Lutzy/LoopHeroThicketSolver/blob/master/src/com/lutzy/loophero/solver/Solver.java
The best my algorithm has found so far yielded 224 points: https://loopherolayout.xyz/?c=111221211211211211212211211212212112112122121211212121121112 but then someone was able to modify that solution by hand to come up with a 234 point solution, but we've still no idea if that's optimal: https://loopherolayout.xyz/?c=111221211211211211211211211211211212212121121112111211211122
(Notice what a valid river looks like, you can't simply create a checkerboard, it must begin on one edge of the region and be contiguous with no backtracking.)
Does anyone have any ideas on if there are possible improvements to this algorithm? I attempted to memoize it by storing the score of the board in a ConcurrentHashMap, but very quickly had a 30 GB+ Map and even for much smaller areas where the non-memoized implementation sped through them, the memoized version struggled.
The only optimization I could think of was that in the case of the 5x14 problem, we only need to exhaustively search 8 unique starting positions, as everything else should be a mirror of these 8 marked positions: https://loopherolayout.xyz?c=111001000010000100001000010000000000000000000000000000000000
I've sort of got a feeling there is no good known algorithm for something like this, but I found the problem interesting enough from a computer science perspective that I wanted to get other peoples' thoughts. Thanks!
1
u/TransientVoltage409 Mar 14 '21
I may not be understanding fully, but it looks like the grid has 2^(MN) possible states, the vast majority of which are not valid. A few basic algorithms would be able to weed out invalid grids quickly. A union-find will identify a non-contiguous river, spanning-tree should reveal any river loops, and...well, the rules for "begin" and "end" seem a little ill-defined, but you might say that a river block, not on an edge, with only one river neighbor, would constitute an endpoint not on the edge and thus an invalid grid.
Not saying this would be fast, but I think there are ways that are much faster than O(n!).