-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.js
More file actions
63 lines (59 loc) · 2.18 KB
/
Maze.js
File metadata and controls
63 lines (59 loc) · 2.18 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
60
61
62
63
const myMaze = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 1, 0, 0, 1, 1, 1],
[0, 1, 0, 0, 1, 0, 0, 1, 0, 0],
[0, 1, 1, 1, 1, 0, 1, 1, 1, 0],
[0, 1, 0, 0, 1, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
];
function mazeResult(maze, startRow, startCol, destRow, destCol) {
let count = 1, pos = [{ row: startRow, col: startCol }];
const rows = maze.length, cols = maze[0].length;
if (maze[startRow][startCol] == 1 && maze[destRow][destCol] == 1) return "NO";
maze[startRow][startCol] = 1;
while (count) {
const newPos = [];
for (let i = 0; i < count; i++) {
if (pos[i].row < rows - 1) {
if (pos[i].row + 1 == destRow && pos[i].col == destCol) return "YES";
if (maze[pos[i].row + 1][pos[i].col] == 0) {
newPos.push({ row: pos[i].row + 1, col: pos[i].col });
maze[pos[i].row + 1][pos[i].col] = 1;
}
};
if (pos[i].row > 0) {
if (pos[i].row - 1 == destRow && pos[i].col == destCol) return "YES";
if (maze[pos[i].row - 1][pos[i].col] == 0) {
newPos.push({ row: pos[i].row - 1, col: pos[i].col });
maze[pos[i].row - 1][pos[i].col] = 1;
}
};
if (pos[i].col < cols - 1) {
if (pos[i].row == destRow && pos[i].col + 1 == destCol) return "YES";
if (maze[pos[i].row][pos[i].col + 1] == 0) {
newPos.push({ row: pos[i].row, col: pos[i].col + 1 });
maze[pos[i].row][pos[i].col + 1] = 1
}
}
if (pos[i].col > 0) {
if (pos[i].row == destRow && pos[i].col - 1 == destCol) return "YES";
if (maze[pos[i].row][pos[i].col - 1] == 0) {
newPos.push({ row: pos[i].row, col: pos[i].col - 1 });
maze[pos[i].row][pos[i].col - 1] = 1;
}
}
}
pos = newPos;
count = pos.length;
}
return "NO";
}
function mazeMatrix(maze, startRow, startCol, destRow, destCol) {
const result = mazeResult(maze, startRow, startCol, destRow, destCol);
console.log(result);
}
mazeMatrix(myMaze, 0, 0, 3, 9);