#C12735. Minimum Steps in a Maze
Minimum Steps in a Maze
Minimum Steps in a Maze
You are given a maze represented by a grid of integers. The grid has R rows and C columns. Each cell is either 0
or 1
. A cell with a value of 0
is passable, while a cell with a value of 1
is blocked. Your task is to find the minimum number of steps required to go from the top-left corner (cell (0, 0)
) to the bottom-right corner (cell (R-1, C-1)
). You can only move in four directions: up, down, left, and right.
If the destination is unreachable, output -1
. Note that if the starting cell is the destination, the answer is 0
.
The number of steps is defined as the number of moves from one cell to an adjacent cell. In mathematical terms, if we let d(u,v) denote the distance from cell u to v via allowed moves, then your task is to compute:
[ \min_{\text{path}} \ d((0,0), (R-1,C-1)) ]
using Breadth-First Search (BFS) or any other suitable algorithm.
inputFormat
The first line contains two integers R and C representing the number of rows and columns, respectively.
This is followed by R lines, each containing C space-separated integers (either 0 or 1) representing the grid.
outputFormat
Output a single integer representing the minimum number of steps from the start to the destination, or -1
if the destination is unreachable.
3 3
0 0 1
0 0 1
1 0 0
4