Module 8 · Graphs and Trees · Dijkstra on Graph

Minimum cost path

Every cell of an n × n grid has a cost to step on. Find the cheapest way from the top-left cell to the bottom-right one, moving up, down, left or right. Treat each cell as a vertex and run Dijkstra: a priority queue always hands back the cheapest cell found so far.

grid holds the cost of each cell, minDis the cheapest cost found so far (∞ while it is null). The queue is a PriorityQueue ordered by minDis: poll() at the front, add() at the rear.