Maze Escape BFS

Graph Traversal Bfs Maze Coding Problem

Problem Statement

You are given a maze represented by a 2D grid. The grid consists of cells where:

  • 0 represents an empty cell
  • 1 represents a wall

Your task is to find the shortest path from the top-left corner (0, 0) to the bottom-right corner (n-1, m-1) using Breadth-First Search (BFS).

Rules:

  • You may move up, down, left, or right (no diagonal moves).
  • You can only step on cells with a value of 0.
  • Return the number of steps in the shortest path. If no path exists, return -1.

Example:

For the maze:

[[0, 1, 0],
 [0, 0, 0],
 [1, 0, 0]]

The shortest path from (0,0) to (2,2) takes 5 steps.

Today, on November 20 (Universal Children’s Day), imagine this problem as guiding children safely through a maze. Use your BFS skills to help them find the escape route!