Skip to content

Commit 934c2b3

Browse files
add: validating-a-maze-path
1 parent 082857d commit 934c2b3

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
- [Finding the maximum depth of a binary tree](finding-the-maximum-depth-of-a-binary-tree)
66
- [Getting the distinct transactions](getting-the-distinct-transactions)
77
- [Replacing with the cipher letters](replacing-with-the-cipher-letters)
8+
- [Validating a maze path](validating-a-maze-path)

validating-a-maze-path/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Validating a maze path
2+
3+
Validate the provided maze path is correct.
4+
5+
e.g.:
6+
7+
Maze matrix
8+
9+
```console
10+
[
11+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
12+
[1, 0, 1, 0, 1, 0, 0, 0, 1, 1],
13+
[1, 0, 1, 0, 1, 0, 1, 0, 0, 1],
14+
[1, 0, 1, 0, 1, 1, 1, 1, 0, 1],
15+
[1, 0, 1, 0, 0, 0, 0, 1, 0, 1],
16+
[1, 0, 1, 0, 1, 0, 1, 1, 0, 1],
17+
[1, 0, 0, 0, 1, 0, 0, 1, 0, 1],
18+
[1, 0, 1, 1, 1, 1, 0, 0, 0, 1],
19+
[1, 0, 1, 0, 0, 0, 0, 1, 0, 1],
20+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
21+
]
22+
```
23+
24+
Input
25+
26+
```console
27+
startRow = 1
28+
startCol = 1
29+
destRow = 2
30+
destCol = 5
31+
```
32+
33+
Output
34+
35+
```console
36+
true
37+
```
38+
39+
## Execute
40+
41+
```bash
42+
node solution.js
43+
```

validating-a-maze-path/solution.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const solution = (
2+
maze,
3+
startRow,
4+
startCol,
5+
destRow,
6+
destCol,
7+
) => {
8+
if (
9+
maze[startRow][startCol] == 1 ||
10+
maze[destRow][destCol] == 1
11+
) {
12+
return false;
13+
}
14+
15+
let slots = [{ row: startRow, col: startCol }];
16+
let tried = [];
17+
let path = false;
18+
19+
const steps = [
20+
{ row: -1, col: 0 },
21+
{ row: +1, col: 0 },
22+
{ row: 0, col: -1 },
23+
{ row: 0, col: +1 },
24+
];
25+
26+
while (slots.length > 0 && !path) {
27+
[...slots].forEach((slot) => {
28+
tried.push(slot);
29+
30+
slots = slots.filter(
31+
(s) => !(s.row == slot.row && s.col == slot.col),
32+
);
33+
34+
for (const step of steps) {
35+
if (
36+
slot.row + step.row == destRow &&
37+
slot.col + step.col == destCol
38+
) {
39+
path = true;
40+
return;
41+
}
42+
}
43+
44+
for (const step of steps) {
45+
const next = {
46+
row: slot.row + step.row,
47+
col: slot.col + step.col,
48+
};
49+
50+
if (
51+
next.row < 0 ||
52+
next.row >= maze.length ||
53+
next.col < 0 ||
54+
next.col >= maze[0].length
55+
) {
56+
continue;
57+
}
58+
59+
if (
60+
tried.find(
61+
(t) => t.row == next.row && t.col == next.col,
62+
)
63+
) {
64+
continue;
65+
}
66+
67+
if (maze[next.row][next.col] == 1) {
68+
continue;
69+
}
70+
71+
slots.push(next);
72+
}
73+
});
74+
}
75+
76+
return path;
77+
};
78+
79+
(() => {
80+
const maze = [
81+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
82+
[1, 0, 1, 0, 1, 0, 0, 0, 1, 1],
83+
[1, 0, 1, 0, 1, 0, 1, 0, 0, 1],
84+
[1, 0, 1, 0, 1, 1, 1, 1, 0, 1],
85+
[1, 0, 1, 0, 0, 0, 0, 1, 0, 1],
86+
[1, 0, 1, 0, 1, 0, 1, 1, 0, 1],
87+
[1, 0, 0, 0, 1, 0, 0, 1, 0, 1],
88+
[1, 0, 1, 1, 1, 1, 0, 0, 0, 1],
89+
[1, 0, 1, 0, 0, 0, 0, 1, 0, 1],
90+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
91+
];
92+
93+
console.log(solution(maze, 1, 1, 2, 5));
94+
})();

0 commit comments

Comments
 (0)