1
var minCost = function (grid) {3
const m = grid[0].length;5
[0, 0], // unused, just put here to make reading signs easier11
const visited = new Set();12
const q = new MinPriorityQueue();15
const { element, priority: changes } = q.dequeue();16
const [x, y] = element;17
if (x === m - 1 && y === n - 1) {20
const key = x + y * m;21
if (visited.has(key)) continue;23
//calculate the direction of the sign and then use it to determine if a change was made whilst pathfinding24
const sign = grid[y][x];25
const sx = dns[sign][0] + x;26
const sy = dns[sign][1] + y;27
for (let i = 1; i < 5; ++i) {28
const nx = dns[i][0] + x;29
const ny = dns[i][1] + y;30
if (grid[ny]?.[nx] && !visited.has(ny * m + nx)) {31
const nextChange = sx === nx && sy === ny ? changes : changes + 1;32
q.enqueue([nx, ny], nextChange);