Maze Prob
Maze Prob
point to a destination in a grid-like structure (maze) where some cells are blocked. Algorithms
like Depth-First Search (DFS) or Breadth-First Search (BFS) can be used to solve this, often
utilizing recursion and stacks. A common approach is to represent the maze as a 2D array
(matrix) where 0s represent walls and 1s represent traversable paths.
2D Array: The maze is typically represented as a 2D array (matrix) where each cell
contains a value indicating whether it's a wall (0) or a path (1).
Recursion: DFS explores the maze by recursively visiting adjacent cells, marking
them as visited to avoid cycles.
Stack (Optional): DFS can be implemented with a stack to keep track of visited
cells and paths, especially when backtracking is needed.
Queue: BFS uses a queue to explore the maze level by level, ensuring the
shortest path is found first.
Efficiency: BFS is generally preferred when finding the shortest path is a primary
concern.
(-1, 0) - North
(-1, 1) - North-East
( 0, 1) - East
( 1, 1) - South-East
( 1, 0) - South
( 1, -1) - South-West
( 0, -1) - West
#include <stdio.h>
#define ROW 5
#define COL 5
int maze[ROW][COL] = {
{1, 0, 1, 1, 1},
{1, 1, 1, 0, 1},
{0, 0, 1, 1, 1},
{1, 1, 0, 0, 1},
{1, 1, 1, 1, 1}
};
int visited[ROW][COL];
return (row >= 0 && col >= 0 && row < ROW && col < COL &&
return 1;
visited[row][col] = 1;
if (isSafe(newRow, newCol)) {
return 1;
return 0;
int main() {
int startRow = 0, startCol = 0;
return 0;
Output:
(4, 4)
(3, 4)
(2, 4)
(2, 3)
(1, 2)
(1, 1)
(1, 0)
(0, 0)