-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrat-in-a-maze.js
156 lines (124 loc) · 3.68 KB
/
rat-in-a-maze.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
class RatMaze {
isSafe(maze, x, y, visited) {
return (x >= 0 && x < maze.length && y >= 0 && y < maze[0].length && maze[x][y] === 1 && visited[x+','+y] === undefined);
}
printPath(maze, startX, startY, endX, endY, paths) {
const m = [...maze];
for (let i = 0; i < paths.length; i++) {
const p = paths[i];
m[p[0]][p[1]] = 4;
}
m[startX][startY] = 2;
m[endX][endY] = 3;
console.log(m);
}
shortestPath(maze, startX, startY, endX, endY) {
if (startX === endX && startY === endY) {
return [];
}
const dirs = [
[-1, 0], // Up
[1, 0], // Down
[0, -1], // Left
[0, 1], // Right
];
const visited = {};
const queue = [{x: startX, y: startY, path: []}];
visited[startX+','+startY] = true;
while (queue.length) {
const curr = queue.shift();
if (curr.x === endX && curr.y === endY) {
return curr.path;
}
for (let i = 0; i < dirs.length; i++) {
const x = curr.x + dirs[i][0];
const y = curr.y + dirs[i][1];
if (this.isSafe(maze, x, y, visited)) {
const path = [...curr.path, [curr.x, curr.y]];
visited[x+','+y] = true;
queue.push({ x: x, y: y, path: path});
}
}
}
return [];
}
solve(maze, startX, startY, endX, endY) {
const solution = new Array(maze.length).fill(null).map(() => new Array(maze[0].length).fill(0));
const visited = {};
if (this.solveUtil(maze, startX, startY, endX, endY, solution, visited)) {
// this.printSolution(solution);
console.log(solution);
return true;
}
console.log('Solution doesn\'t exist');
return false;
}
solveUtil(maze, x, y, endX, endY, solution, visited) {
if (x === endX && y === endY) {
solution[x][y] = 1;
return true;
}
if (!this.isSafe(maze, x, y, visited)) {
return false;
}
solution[x][y] = 1;
visited[x+','+y] = true;
// Move up
if (this.solveUtil(maze, x - 1, y, endX, endY, solution, visited)) {
return true;
}
// Move down
if (this.solveUtil(maze, x + 1, y, endX, endY, solution, visited)) {
return true;
}
// // Move left
if (this.solveUtil(maze, x, y - 1, endX, endY, solution, visited)) {
return true;
}
// // Move right
if (this.solveUtil(maze, x, y + 1, endX, endY, solution, visited)) {
return true;
}
solution[x][y] = 0;
return false;
}
printSolution(solution) {
for (let i = 0; i < solution.length; i++) {
console.log(solution[i].map((e) => e === 0 ? ' ' : 'E'));
}
}
}
const maze = [
[1,1,0,0,0,0,0,0,0,0],
[1,0,1,1,1,1,1,1,1,0],
[1,1,1,0,0,0,0,0,1,1],
[1,1,0,0,1,1,1,1,1,0],
[1,0,1,1,1,0,0,0,1,1],
[1,0,1,0,0,1,1,1,0,1],
[1,0,1,0,0,1,0,1,0,0],
[1,0,1,1,1,1,0,1,1,1]
]
const ratMaze = new RatMaze();
console.time('solve');
ratMaze.solve(maze, 0, 0, 7, 9);
console.timeEnd('solve');
console.log('');
ratMaze.solve(maze, 5, 5, 7, 0);
console.log('');
console.time('path');
const paths = ratMaze.shortestPath(maze, 0, 0, 7, 9);
console.timeEnd('path');
ratMaze.printPath(maze, 0, 0, 7, 9, paths);
console.log('------------');
const maze2 = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0],
[0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
];
const paths2 = ratMaze.shortestPath(maze2, 6, 2, 1, 14);
ratMaze.printPath(maze2, 6, 2, 1, 14, paths2);