Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Minimum Cost to Make at Least One Valid Path in a Grid
Bookmark
Dijkstra
0-1 BFS
Input
4x4 (ans: 3)
3x3 (ans: 0)
2x2 (ans: 1)
Custom
grid
=
[[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]
→
→
→
→
←
←
←
←
→
→
→
→
←
←
←
←
min-heap (cheapest first)
Default
Active
Visited
Match
Differs
→
→
→
→
←
←
←
←
→
→
→
→
←
←
←
←
min-heap (cheapest first)
Default
Active
Visited
Match
Differs
→
d=0
→
d=0
→
d=0
→
←
d=1
←
←
←
→
→
→
→
←
←
←
←
→ points right, moving left costs 1
min-heap (cheapest first)
(0,2) d=0
(1,0) d=1
front
rear
Default
Active
Visited
Match
Differs
→
d=0
→
d=0
→
d=0
→
d=0
←
d=1
←
d=1
←
d=1
←
→
→
→
→
←
←
←
←
min-heap (cheapest first)
(1,0) d=1
(1,1) d=1
(1,2) d=1
front
rear
Default
Active
Visited
Match
Differs
→
d=0
→
d=0
→
d=0
→
d=0
←
d=1
←
d=1
←
d=1
←
d=1
→
d=2
→
→
→
←
←
←
←
min-heap (cheapest first)
(1,2) d=1
(1,3) d=1
(2,0) d=2
front
rear
Default
Active
Visited
Match
Differs
→
d=0
→
d=0
→
d=0
→
d=0
←
d=1
←
d=1
←
d=1
←
d=1
→
d=2
→
d=2
→
→
←
←
←
←
min-heap (cheapest first)
(1,3) d=1
(2,0) d=2
(2,1) d=2
front
rear
Default
Active
Visited
Match
Differs
→
d=0
→
d=0
→
d=0
→
d=0
←
d=1
←
d=1
←
d=1
←
d=1
→
d=2
→
d=2
→
d=2
→
d=2
←
←
←
←
min-heap (cheapest first)
(2,1) d=2
(2,2) d=2
(2,3) d=2
front
rear
Default
Active
Visited
Match
Differs
→
d=0
→
d=0
→
d=0
→
d=0
←
d=1
←
d=1
←
d=1
←
d=1
→
d=2
→
d=2
→
d=2
→
d=2
←
d=3
←
d=3
←
←
→ points right, moving down costs 1
min-heap (cheapest first)
(2,2) d=2
(2,3) d=2
(3,0) d=3
(3,1) d=3
front
rear
Default
Active
Visited
Match
Differs
→
d=0
→
d=0
→
d=0
→
d=0
←
d=1
←
d=1
←
d=1
←
d=1
→
d=2
→
d=2
→
d=2
→
d=2
←
d=3
←
d=3
←
d=3
←
→ points right, moving left costs 1
min-heap (cheapest first)
(3,0) d=3
(3,1) d=3
(3,2) d=3
front
rear
Default
Active
Visited
Match
Differs
→
d=0
→
d=0
→
d=0
→
d=0
←
d=1
←
d=1
←
d=1
←
d=1
→
d=2
→
d=2
→
d=2
→
d=2
←
d=3
←
d=3
←
d=3
←
d=3
← points left, moving right costs 1
min-heap (cheapest first)
(3,2) d=3
(3,3) d=3
front
rear
Default
Active
Visited
Match
Differs
→
d=0
→
d=0
→
d=0
→
d=0
←
d=1
←
d=1
←
d=1
←
d=1
→
d=2
→
d=2
→
d=2
→
d=2
←
d=3
←
d=3
←
d=3
←
d=3
min-heap (cheapest first)
Default
Active
Visited
Match
Differs
algo
master
.
io
Step:
A 4x4 grid of arrows. Following an arrow is free, turning costs 1.
0 / 186
Input
4x4 (ans: 3)
3x3 (ans: 0)
2x2 (ans: 1)
Custom
grid
=
[[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]
0 / 186
algo
master
.
io
Step:
A 4x4 grid of arrows. Following an arrow is free, turning costs 1.