Free steps go to the front
transcript
A grid. Some cells are free. Some cost one to enter. You need the cheapest walk. Do you need a heap? No. A deque is enough, and it is simpler.
Only two costs exist. Zero for an empty cell. One for a stand. No other cost ever appears.
A deque holds costs, front to back. Take the front cell. A free neighbour goes to the front. A stand goes to the back. The order never breaks.
The deque is in order of cost. So the front is final. A heap would only sort again. So skip the heap here.
A heap pays sixteen levels here. A deque pays one step.
A plain queue looks tempting. It orders cells by steps. But stands change the cost. So it can return the wrong count.
Match the queue to the costs. Free to the front. Costly to the back. The front is always final. Full breakdown in the lecture.