✅ JS || Multiple Approaches || Easy to understand
I just found this Blog and Github repository with solutions to Leetcode problems.\nhttps://leet-codes.blogspot.com/2022/11/nearest-exit-from-entrance-in-maze.ht
I just found this Blog and Github repository with solutions to Leetcode problems.\nhttps://leet-codes.blogspot.com/2022/11/nearest-exit-from-entrance-in-maze.html\nIt has solutions to almost every problem on Leetcode, and I recommend checking it out.\nNote: You can bookmark it as a resource, and approach. Other approaches are in above blog\n<br>\n\n```\n/**\n * @param {character[][]} maze\n * @param {number[]} entrance\n * @return {number}\n */\nconst dx = [1, -1, 0, 0], dy = [0, 0, 1, -1];\nconst nearestExit = (g, entrance) => {\n    let [n, m, ex, ey] = [g.length, g[0].length, entrance[0], entrance[1]];\n    let dis = initialize2DArrayNew(n, m);\n    let q = [];\n    for (let i = 0; i < n; i++) { // find all exits\n        for (let j = 0; j < m; j++) {\n            if ((i == ex && j == ey) || g[i][j] == \'+\') continue;\n            if (i == 0 || i == n - 1 || j == 0 || j == m - 1) {\n                q.push([i, j]);\n                dis[i][j] = 0; // reset to 0 for calculating the min path\n            }\n        }\n    }\n    while (q.length) { // bfs\n        let cur = q.shift();\n        let [x, y] = cur;\n        for (let k = 0; k < 4; k++) {\n            let xx = x + dx[k];\n            let yy = y + dy[k];\n            if (xx < 0 || xx >= n || yy < 0 || yy >= m || g[xx][yy] == \'+\') continue; // out of bound or wall\n            if (dis[xx][yy] > dis[x][y] + 1) { // update min path\n                dis[xx][yy] = dis[x][y] + 1;\n                q.push([xx, yy]);\n            }\n        }\n    }\n    let res = dis[ex][ey];\n    return res == Number.MAX_SAFE_INTEGER ? -1 : res;\n};\n\nconst initialize2DArrayNew = (n, m) => { \n    let data = []; \n    for (let i = 0; i < n; i++) { \n        let tmp = Array(m).fill(Number.MAX_SAFE_INTEGER); \n        data.push(tmp); \n    } \n    return data; \n};\n```
['JavaScript']