Solved LeetCode program · 635 of 1,000
#0864 · Shortest Path to Get All Keys
Original English source
Keep a shared grid holding 2, 0, 3, 0, 0, 1, 1, 1, 0, 1, 4, 0, 5, 0, and 6. Keep a shared visited as a list of 60 zeros. Keep a shared queued cells as an empty list of whole numbers. Keep a shared queued masks as an empty list of whole numbers. Keep a shared queued distances as an empty list of whole numbers. Keep a shared count of 0. Keep a shared head of 0. Keep a shared answer of 0. Keep a shared activemask of 0. Keep a shared activetravel of 0.
To visit from target (a whole number). The cell is item target of grid. If cell is 1, return. The gained is activemask. If cell is 3, set gained to bitwise or of activemask and 1. If cell is 4, set gained to bitwise or of activemask and 2. The keyone is bitwise and of activemask and 1. The keytwo is bitwise and of activemask and 2. If cell is 5 and keyone is 0, return. If cell is 6 and keytwo is 0, return. The mark is target times 4 plus gained. If item mark of visited is 1, return. Set item mark of visited to 1. Place target into queued cells. Place gained into queued masks. Place activetravel plus 1 into queued distances. Add 1 to count.
Set item 0 of visited to 1. Place 0 into queued cells. Place 0 into queued masks. Place 0 into queued distances. Set count to 1. Set head to 0. Set answer to -1. The columns is 5. The rows is 3. The lastrow is rows minus 1. The lastcolumn is columns minus 1.
Repeat while head is less than count.
The cursor is head. The spot is item cursor of queued cells. Set activemask to item cursor of queued masks. Set activetravel to item cursor of queued distances. Add 1 to head. If activemask is 3, set answer to activetravel, stop. The line is spot divided by columns. The column is spot modulo columns. If line is more than 0, Visit from spot minus columns. If line is less than lastrow, Visit from spot plus columns. If column is more than 0, Visit from spot minus 1. If column is less than lastcolumn, Visit from spot plus 1.
Show answer.
Expected stdout
8