-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeSolver.ts
More file actions
54 lines (42 loc) 路 1.29 KB
/
Copy pathMazeSolver.ts
File metadata and controls
54 lines (42 loc) 路 1.29 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
const dir = [
[0, -1], // up
[1, 0], // right
[0, 1], // down
[-1, 0], // left
];
function walk(maze: string[], wall: string, curr: Point, end: Point, seen: boolean[][], path: Point[]): boolean {
// 1. Base case
// off the map
if (curr.x < 0 || curr.x >= maze[0].length || curr.y < 0 || curr.y >= maze.length) return false;
// on a wall
if (maze[curr.y][curr.x] === wall) return false;
// at the end
if (curr.x === end.x && curr.y === end.y) {
path.push(curr);
return true;
}
// seen before
if (seen[curr.y][curr.x]) return false;
// 2. Recursive case
// // pre
seen[curr.y][curr.x] = true;
path.push(curr);
// // recurse
for (let i = 0; i < dir.length; ++i) {
const [dx, dy] = dir[i];
const next = { x: curr.x + dx, y: curr.y + dy };
if (walk(maze, wall, next, end, seen, path)) return true;
}
// // post
path.pop();
return false;
}
export default function solve(maze: string[], wall: string, start: Point, end: Point): Point[] {
const seen: boolean[][] = [];
const path: Point[] = [];
for (let i = 0; i < maze.length; ++i) {
seen.push(new Array(maze[i].length).fill(false));
}
walk(maze, wall, start, end, seen, path);
return path;
}