Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding path to the end of maze using JavaScript
Problem
We are required to write a JavaScript function that takes in a matrix of N * N order. The walls in the matrix are marked by 'W' and empty positions are marked by '_'.
We can move in any of the four directions at any point. Our function should return true if we can reach the end position [N - 1, N - 1] from the starting position [0, 0], false otherwise.
Algorithm Approach
We'll use a Breadth-First Search (BFS) algorithm to find if a path exists from the starting position to the end position. BFS explores all possible paths level by level, making it ideal for finding the shortest path in an unweighted maze.
Example
Following is the corrected code:
const maze = [
['_', 'W', 'W', 'W'],
['_', 'W', 'W', 'W'],
['W', '_', '_', 'W'],
['W', 'W', 'W', '_']
];
const canFindPath = (m = []) => {
if (!m || !m.length || !m[0].length) return false;
let h = m.length;
let w = m[0].length;
// Create a copy to avoid modifying original maze
let visited = Array(h).fill().map(() => Array(w).fill(false));
// BFS queue starting from [0, 0]
let queue = [[0, 0]];
visited[0][0] = true;
// Four directions: up, down, left, right
const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];
while (queue.length > 0) {
let [x, y] = queue.shift();
// Check if we reached the end
if (x === h - 1 && y === w - 1) {
return true;
}
// Explore all four directions
for (let [dx, dy] of directions) {
let newX = x + dx;
let newY = y + dy;
// Check bounds and if cell is valid
if (newX >= 0 && newX < h &&
newY >= 0 && newY < w &&
!visited[newX][newY] &&
m[newX][newY] === '_') {
visited[newX][newY] = true;
queue.push([newX, newY]);
}
}
}
return false;
};
console.log(canFindPath(maze));
Output
false
How It Works
The algorithm works as follows:
- Initialization: Create a visited array to track explored cells and a queue for BFS
- BFS Traversal: Start from position [0, 0] and explore all reachable empty cells ('_')
- Direction Check: For each position, check all four adjacent cells (up, down, left, right)
- Boundary Validation: Ensure new positions are within maze bounds and not walls or already visited
- Path Found: Return true if we reach the end position [N-1, N-1], false if queue is empty
Testing Different Maze
// A maze with a valid path
const solvableMaze = [
['_', '_', 'W', 'W'],
['W', '_', 'W', 'W'],
['W', '_', '_', 'W'],
['W', 'W', '_', '_']
];
console.log("Solvable maze:", canFindPath(solvableMaze));
// The original maze (no valid path)
const originalMaze = [
['_', 'W', 'W', 'W'],
['_', 'W', 'W', 'W'],
['W', '_', '_', 'W'],
['W', 'W', 'W', '_']
];
console.log("Original maze:", canFindPath(originalMaze));
Output
Solvable maze: true Original maze: false
Conclusion
This BFS-based maze solver efficiently determines if a path exists from start to end. The algorithm has O(N²) time complexity and guarantees finding a path if one exists.
