Skip to content

Commit 9948234

Browse files
Merge pull request #66 from indrasuthar07/maze-solver
feat: Maze Solver Visualizer
2 parents ca852cf + e46b53f commit 9948234

File tree

6 files changed

+537
-2
lines changed

6 files changed

+537
-2
lines changed

package-lock.json

Lines changed: 17 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Homepage from "./pages/Homepage.jsx";
77
import DSPage from "./pages/dataStructure/datastructurePage.jsx"
88
import DynamicProgrammingPage from "./pages/dynamic-programming/DyanmicProgrammingPage.jsx";
99
import Searchingpage from "./pages/searching/searchingPage";
10+
import RecursionPage from "./pages/Recursion/RecursionPage";
1011
function App() {
1112
return (
1213
<Router>
@@ -18,6 +19,7 @@ function App() {
1819
<Route path="/data-structures" element={<DSPage/>}/>
1920
<Route path="/graph" element={<GraphPage />} />
2021
<Route path="/dynamic-programming" element={<DynamicProgrammingPage />} />
22+
<Route path="/recursion" element={<RecursionPage/>}/>
2123
</Routes>
2224
</Router>
2325
);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
export function mazeSolverSteps(grid, start, end, allowDiagonal = false) {
2+
const rows = grid.length;
3+
const cols = grid[0].length;
4+
5+
const steps = [];
6+
const visited = Array.from({ length: rows }, () => Array(cols).fill(false));
7+
const parent = Array.from({ length: rows }, () =>
8+
Array(cols).fill(null)
9+
);
10+
11+
const directions = allowDiagonal ? [[0, 1], [1, 0], [0, -1], [-1, 0],[1, 1], [1, -1], [-1, 1], [-1, -1],]
12+
: [[0, 1], [1, 0], [0, -1], [-1, 0]];
13+
14+
const queue = [];
15+
queue.push(start);
16+
visited[start[0]][start[1]] = true;
17+
18+
let solutionFound = false;
19+
20+
while (queue.length > 0){
21+
const [r, c] = queue.shift();
22+
//exploring cell
23+
steps.push({ type: "explore", row: r, col: c, visited: visited.map(v => [...v]),
24+
grid: cloneGrid(grid),
25+
message: `Exploring cell (${r}, ${c}).`,
26+
});
27+
28+
if (r === end[0] && c === end[1]) {
29+
solutionFound = true;
30+
steps.push({ type: "found",row: r,col: c,visited: visited.map(v => [...v]),
31+
grid: cloneGrid(grid),
32+
message: `Reached end (${r}, ${c}) — shortest path found!`,
33+
});
34+
break;
35+
}
36+
37+
for (const [dr, dc] of directions) {
38+
const nr = r + dr;
39+
const nc = c + dc;
40+
41+
if(nr>=0 &&nc >= 0 && nr<rows && nc<cols && grid[nr][nc]!==1 && !visited[nr][nc]) {
42+
visited[nr][nc] = true;
43+
parent[nr][nc] = [r, c];
44+
queue.push([nr, nc]);
45+
46+
steps.push({ type: "enqueue", row: nr,col: nc, visited: visited.map(v => [...v]),
47+
grid: cloneGrid(grid),
48+
message: `Adding cell (${nr}, ${nc}) to queue.`,
49+
});
50+
}
51+
}
52+
}
53+
//reconstruct shortest path
54+
const path = [];
55+
if (solutionFound) {
56+
let node = end;
57+
while (node) {
58+
path.unshift(node);
59+
node = parent[node[0]][node[1]];
60+
}
61+
62+
for (const [r, c] of path) {
63+
steps.push({type: "path_cell",row: r,col: c,visited: visited.map(v => [...v]),
64+
grid: cloneGrid(grid),
65+
path: [...path],
66+
message: `Cell (${r}, ${c}) is part of the shortest path.`,
67+
});
68+
}
69+
} else {
70+
steps.push({ type: "no_path", row: null,col: null,visited: visited.map(v => [...v]),
71+
grid: cloneGrid(grid),
72+
message: "No path possible between Start and End points.",
73+
});
74+
}
75+
76+
return {steps,solutionFound,path};
77+
}
78+
79+
function cloneGrid(grid) {
80+
return grid.map(row => [...row]);
81+
}

0 commit comments

Comments
 (0)