Four-way weighted maze
Grid Algorithms
In a 20x20 grid with vertices (r,c), 0<=r,c<=19, entering vertex (r,c) costs 1+(17r+31c+7rc) mod 91. Starting at (0,0) for free, reach (19,19) with 4-neighbor moves, possibly revisiting vertices. Find the least total entering cost.
Solve using mathematics, algorithms, or your own code offline. Submit only the final integer here—no program, expression, graph, or written proof.
Check your answer without signing in. Sign in to save your progress.
How to approach this problem
Use Dijkstra’s shortest path algorithm with a priority queue. The cost of an edge is the destination cell’s entering cost. Four-way motion is not acyclic, so monotone grid DP is insufficient.
Worked example with different values
If a direct entry costs 10 but a three-edge detour costs 2+2+1, the distance is 5. Positive costs permit Dijkstra’s finalized-distance rule.
Apply the same reasoning to the challenge. This example is a separate practice problem.
Learn the technique: Dynamic programming and grid paths