2
* @param {number[][]} grid5
var minimumMoves = function (grid) {6
const len = grid.length;7
const initPosition = [0, true, 1];8
const visited = new Set([initPosition.join()]);10
const queue = new Queue();12
queue.enqueue(initPosition);13
while (!queue.isEmpty()) {14
let [row, isHorizontal, col, numMoves] = queue.dequeue();15
if (row + col === len * 2 - 2 && isHorizontal) return numMoves;19
if (grid[row][col + 1] === 0) {20
positions.push([row, true, col + 1]);24
grid[row + 1][col - 1] === 0 &&25
grid[row + 1][col] === 027
positions.push([row + 1, true, col]);28
positions.push([row + 1, false, col - 1]);31
if (row + 1 < len && grid[row + 1][col] === 0) {32
positions.push([row + 1, false, col]);34
if (row > 0 && grid[row - 1][col + 1] === 0 && grid[row][col + 1] === 0) {35
positions.push([row, false, col + 1]);36
positions.push([row - 1, true, col + 1]);39
for (let position of positions) {40
const str = position.join();41
if (visited.has(str)) continue;43
position.push(numMoves);44
queue.enqueue(position);52
this.head = { next: null };53
this.tail = this.head;57
return this.head.next === null;61
const { value } = this.head.next;62
this.head = this.head.next;67
this.tail.next = { value, next: null };68
this.tail = this.tail.next;