-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.js
61 lines (47 loc) · 1.21 KB
/
default.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* @lc app=leetcode.cn id=1926 lang=javascript
* @lcpr version=30204
*
* [1926] 迷宫中离入口最近的出口
*/
// @lcpr-template-start
// @lcpr-template-end
// @lc code=start
/**
* @param {character[][]} maze
* @param {number[]} entrance
* @return {number}
*/
var nearestExit = function(maze, entrance) {
const queue = [entrance];
let queue_i = 0;
let step = 0;
while (queue_i < queue.length) {
let currStepLength = queue.length - queue_i;
let currStepIndex = queue_i + currStepLength;
while (queue_i < currStepIndex) {
const [i, j] = queue[queue_i++];
if (i < 0 || j < 0 || i >= maze.length || j >= maze[0].length) {
if (step > 1) return step - 1;
else continue;
}
if (maze[i][j] === '+') continue;
maze[i][j] = '+';
[[0, 1], [0, -1], [1, 0], [-1, 0]].forEach(([di, dj]) => {
queue.push([i + di, j + dj]);
})
}
step++;
}
return -1;
};
// @lc code=end
// @lcpr case=start
// [["+","+",".","+"],[".",".",".","+"],["+","+","+","."]]\n[1,2]\n
// @lcpr case=end
// @lcpr case=start
// [["+","+","+"],[".",".","."],["+","+","+"]]\n[1,0]\n
// @lcpr case=end
// @lcpr case=start
// [[".","+"]]\n[0,0]\n
// @lcpr case=end