-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0490-the-maze.js
More file actions
59 lines (47 loc) · 2.02 KB
/
0490-the-maze.js
File metadata and controls
59 lines (47 loc) · 2.02 KB
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
/**
* The Maze
* Time Complexity: O(rows * cols * (rows + cols))
* Space Complexity: O(rows * cols)
*/
var hasPath = function (mazeGrid, startCoordinates, destinationTarget) {
const gridRows = mazeGrid.length;
const gridCols = mazeGrid[0].length;
const visitQueue = [];
visitQueue.push(startCoordinates);
const visitedPoints = new Set();
visitedPoints.add(`${startCoordinates[0]},${startCoordinates[1]}`);
const deltaRows = [-1, 1, 0, 0];
const deltaCols = [0, 0, -1, 1];
while (visitQueue.length > 0) {
const currentMazePosition = visitQueue.shift();
const currentMazeRow = currentMazePosition[0];
const currentMazeCol = currentMazePosition[1];
if (currentMazeRow === destinationTarget[0] && currentMazeCol === destinationTarget[1]) {
return true;
}
for (let directionIndex = 0; directionIndex < 4; ++directionIndex) {
const currentDeltaRow = deltaRows[directionIndex];
const currentDeltaCol = deltaCols[directionIndex];
let potentialNewRow = currentMazeRow;
let potentialNewCol = currentMazeCol;
while (true) {
const nextStepRow = potentialNewRow + currentDeltaRow;
const nextStepCol = potentialNewCol + currentDeltaCol;
const isWithinBounds = nextStepRow >= 0 && nextStepRow < gridRows && nextStepCol >= 0 && nextStepCol < gridCols;
const isNotWall = isWithinBounds && mazeGrid[nextStepRow][nextStepCol] === 0;
if (isNotWall) {
potentialNewRow = nextStepRow;
potentialNewCol = nextStepCol;
} else {
break;
}
}
const stopPositionKey = `${potentialNewRow},${potentialNewCol}`;
if (!visitedPoints.has(stopPositionKey)) {
visitedPoints.add(stopPositionKey);
visitQueue.push([potentialNewRow, potentialNewCol]);
}
}
}
return false;
};