Solved LeetCode program · 884 of 1,000
#1210 · Minimum Moves to Reach Target with Rotations
Original English source
Keep a shared grid holding 0, 0, 0, and 0. Keep a shared seen as an empty list of whole numbers. Keep a shared queue as an empty list of whole numbers. Keep a shared distances as an empty list of whole numbers. Keep a shared last of 1. Keep a shared count of 0.
For every spot from 0 to 15.
Place 0 into seen.
To enqueue from state (a whole number) with distance (a whole number). If item state of seen is 1, return. Set item state of seen to 1. Place state into queue. Place distance into distances. Add 1 to count.
To turn from state (a whole number) with distance (a whole number). The headcol is state modulo 2. The rest is state divided by 2. The headrow is rest modulo 2. The rest is rest divided by 2. The tailcol is rest modulo 2. The tailrow is rest divided by 2. The horizontal is 0. If tailrow is headrow and headcol is tailcol plus 1, set horizontal to 1.
If horizontal is 1 and headrow is less than last.
The cell is item (headrow times 2 plus headcol plus 2) of grid. The fresh is ((headrow times 2 plus headcol) times 2 plus headrow plus 1) times 2 plus headcol. If cell is 0, Enqueue from fresh with distance plus 1.
To bend from state (a whole number) with distance (a whole number). The headcol is state modulo 2. The rest is state divided by 2. The headrow is rest modulo 2. The rest is rest divided by 2. The tailcol is rest modulo 2. The tailrow is rest divided by 2. The vertical is 0. If tailcol is headcol and headrow is tailrow plus 1, set vertical to 1.
If vertical is 1 and headcol is more than 0.
The cell is item (headrow times 2 plus headcol minus 1) of grid. The fresh is ((headrow times 2 plus headcol) times 2 plus headrow) times 2 plus headcol minus 1. If cell is 0, Enqueue from fresh with distance plus 1.
To advance from state (a whole number) with distance (a whole number). The headcol is state modulo 2. The rest is state divided by 2. The headrow is rest modulo 2. The rest is rest divided by 2. The tailcol is rest modulo 2. The tailrow is rest divided by 2. The horizontal is 0. If tailrow is headrow and headcol is tailcol plus 1, set horizontal to 1.
If horizontal is 1 and headcol is less than last.
The cell is item (headrow times 2 plus headcol plus 1) of grid. The fresh is ((tailrow times 2 plus tailcol plus 1) times 2 plus headrow) times 2 plus headcol plus 1. If cell is 0, Enqueue from fresh with distance plus 1.
To descend from state (a whole number) with distance (a whole number). The headcol is state modulo 2. The rest is state divided by 2. The headrow is rest modulo 2. The rest is rest divided by 2. The tailcol is rest modulo 2. The tailrow is rest divided by 2. The vertical is 0. If tailcol is headcol and headrow is tailrow plus 1, set vertical to 1.
If vertical is 1 and headrow is less than last.
The cell is item (headrow times 2 plus headcol plus 2) of grid. The fresh is ((tailrow times 2 plus tailcol plus 2) times 2 plus headrow plus 1) times 2 plus headcol. If cell is 0, Enqueue from fresh with distance plus 1.
Set item 1 of seen to 1. Place 1 into queue. Place 0 into distances. Set count to 1. The head is 0. The answer is -1.
Repeat while head is less than count.
The state is item head of queue. The travel is item head of distances. Add 1 to head. If state is 11 or state is 14, set answer to travel, stop. Turn from state with travel. Bend from state with travel. Advance from state with travel. Descend from state with travel.
Show answer.
Expected stdout
2