|
| 1 | +/** |
| 2 | + * @param {string[]} matrix |
| 3 | + * @return {number} |
| 4 | + */ |
| 5 | +var minMoves = function (matrix) { |
| 6 | + const n = matrix.length, |
| 7 | + m = matrix[0].length |
| 8 | + |
| 9 | + const cells = {} |
| 10 | + for (let i = 0; i < n; i++) { |
| 11 | + for (let j = 0; j < m; j++) { |
| 12 | + if (matrix[i][j] !== '.' && matrix[i][j] !== '#') { |
| 13 | + if (!cells[matrix[i][j]]) { |
| 14 | + cells[matrix[i][j]] = [] |
| 15 | + } |
| 16 | + cells[matrix[i][j]].push([i, j]) |
| 17 | + } |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + if (matrix[n - 1][m - 1] === '#') return -1 |
| 22 | + |
| 23 | + const pq = new PQ((a, b) => a[0] < b[0]) |
| 24 | + const dist = Array.from({ length: n }, () => Array(m).fill(Infinity)) |
| 25 | + const used = new Set() |
| 26 | + |
| 27 | + pq.push([0, 0, 0]) |
| 28 | + dist[0][0] = 0 |
| 29 | + |
| 30 | + const dx = [0, 0, -1, 1] |
| 31 | + const dy = [-1, 1, 0, 0] |
| 32 | + |
| 33 | + while (!pq.isEmpty()) { |
| 34 | + const [curDist, x, y] = pq.pop() |
| 35 | + |
| 36 | + if (curDist > dist[x][y]) continue |
| 37 | + if (x === n - 1 && y === m - 1) return curDist |
| 38 | + |
| 39 | + if ( |
| 40 | + matrix[x][y].toUpperCase() === matrix[x][y] && |
| 41 | + !used.has(matrix[x][y]) |
| 42 | + ) { |
| 43 | + used.add(matrix[x][y]) |
| 44 | + |
| 45 | + for (const [newX, newY] of cells[matrix[x][y]] || []) { |
| 46 | + if (curDist < dist[newX][newY]) { |
| 47 | + dist[newX][newY] = curDist |
| 48 | + pq.push([curDist, newX, newY]) |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + for (let k = 0; k < 4; k++) { |
| 54 | + const nextX = x + dx[k], |
| 55 | + nextY = y + dy[k] |
| 56 | + |
| 57 | + if ( |
| 58 | + isValid(nextX, nextY, n, m, matrix) && |
| 59 | + curDist + 1 < dist[nextX][nextY] |
| 60 | + ) { |
| 61 | + dist[nextX][nextY] = curDist + 1 |
| 62 | + pq.push([curDist + 1, nextX, nextY]) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return -1 |
| 68 | +} |
| 69 | +function isValid(i, j, n, m, matrix) { |
| 70 | + if (i < 0 || j < 0 || i >= n || j >= m) return false |
| 71 | + if (matrix[i][j] === '#') return false |
| 72 | + return true |
| 73 | +} |
| 74 | + |
| 75 | +class PQ { |
| 76 | + constructor(comparator = (a, b) => a > b) { |
| 77 | + this.heap = [] |
| 78 | + this.top = 0 |
| 79 | + this.comparator = comparator |
| 80 | + } |
| 81 | + size() { |
| 82 | + return this.heap.length |
| 83 | + } |
| 84 | + isEmpty() { |
| 85 | + return this.size() === 0 |
| 86 | + } |
| 87 | + peek() { |
| 88 | + return this.heap[this.top] |
| 89 | + } |
| 90 | + push(...values) { |
| 91 | + values.forEach((value) => { |
| 92 | + this.heap.push(value) |
| 93 | + this.siftUp() |
| 94 | + }) |
| 95 | + return this.size() |
| 96 | + } |
| 97 | + pop() { |
| 98 | + const poppedValue = this.peek() |
| 99 | + const bottom = this.size() - 1 |
| 100 | + if (bottom > this.top) { |
| 101 | + this.swap(this.top, bottom) |
| 102 | + } |
| 103 | + this.heap.pop() |
| 104 | + this.siftDown() |
| 105 | + return poppedValue |
| 106 | + } |
| 107 | + replace(value) { |
| 108 | + const replacedValue = this.peek() |
| 109 | + this.heap[this.top] = value |
| 110 | + this.siftDown() |
| 111 | + return replacedValue |
| 112 | + } |
| 113 | + |
| 114 | + parent = (i) => ((i + 1) >>> 1) - 1 |
| 115 | + left = (i) => (i << 1) + 1 |
| 116 | + right = (i) => (i + 1) << 1 |
| 117 | + greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) |
| 118 | + swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) |
| 119 | + siftUp = () => { |
| 120 | + let node = this.size() - 1 |
| 121 | + while (node > this.top && this.greater(node, this.parent(node))) { |
| 122 | + this.swap(node, this.parent(node)) |
| 123 | + node = this.parent(node) |
| 124 | + } |
| 125 | + } |
| 126 | + siftDown = () => { |
| 127 | + let node = this.top |
| 128 | + while ( |
| 129 | + (this.left(node) < this.size() && this.greater(this.left(node), node)) || |
| 130 | + (this.right(node) < this.size() && this.greater(this.right(node), node)) |
| 131 | + ) { |
| 132 | + let maxChild = |
| 133 | + this.right(node) < this.size() && |
| 134 | + this.greater(this.right(node), this.left(node)) |
| 135 | + ? this.right(node) |
| 136 | + : this.left(node) |
| 137 | + this.swap(node, maxChild) |
| 138 | + node = maxChild |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments