forked from loiane/javascript-datastructures-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrat-in-maze.js
41 lines (39 loc) · 882 Bytes
/
rat-in-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
function isSafe(maze, x, y) {
const n = maze.length;
if (x >= 0 && y >= 0 && x < n && y < n && maze[x][y] !== 0) {
return true;
}
return false;
}
function findPath(maze, x, y, solution) {
const n = maze.length;
if (x === n - 1 && y === n - 1) {
solution[x][y] = 1;
return true;
}
if (isSafe(maze, x, y) === true) {
solution[x][y] = 1;
if (findPath(maze, x + 1, y, solution)) {
return true;
}
if (findPath(maze, x, y + 1, solution)) {
return true;
}
solution[x][y] = 0;
return false;
}
return false;
}
export function ratInAMaze(maze) {
const solution = [];
for (let i = 0; i < maze.length; i++) {
solution[i] = [];
for (let j = 0; j < maze[i].length; j++) {
solution[i][j] = 0;
}
}
if (findPath(maze, 0, 0, solution) === true) {
return solution;
}
return 'NO PATH FOUND';
}