Solved LeetCode program · 991 of 1,000
#1368 · Minimum Cost to Make at Least One Valid Path in a Grid
Original English source
Keep a shared grid holding 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, and 2. Keep a shared distance as an empty list of whole numbers.
For every spot from 0 to 15.
Place 1000 into distance.
Set item 0 of distance to 0. Keep a shared here of 0.
To relax from target (a whole number) with cost (a whole number). If target is less than 0, return. If target is more than 15, return. If item target of distance is more than here plus cost, set item target of distance to here plus cost.
Repeat 40 times.
For every cell from 0 to 15.
The row is cell divided by 4. The column is cell modulo 4. Set here to item cell of distance. The rightcost is 1. The leftcost is 1. The downcost is 1. The upcost is 1. If item cell of grid is 1, set rightcost to 0. If item cell of grid is 2, set leftcost to 0. If item cell of grid is 3, set downcost to 0. If item cell of grid is 4, set upcost to 0. If column is less than 3, Relax from cell plus 1 with rightcost. If column is more than 0, Relax from cell minus 1 with leftcost. If row is less than 3, Relax from cell plus 4 with downcost. If row is more than 0, Relax from cell minus 4 with upcost.
Show item 15 of distance.
Expected stdout
3