Maze Search
InterviewTime limit1sMemory limit192 MB
Find the minimum number of cells traversed on a grid path from top-left to bottom-right using BFS shortest path.
Problem
You are given a maze with N rows and M columns. Each cell contains either 1 or 0. A cell with 1 can be entered, and a cell with 0 cannot be entered.
Starting from position (1, 1), find the minimum number of cells that must be passed through to reach position (N, M). You may move only to an adjacent cell sharing an edge. The starting cell and the destination cell are both included in the count.
Input
The first line contains two integers N and M (2 <= N, M <= 100). Each of the next N lines contains M digits describing the maze. The digits are given without spaces.
Output
Print the minimum number of cells that must be passed through. The input is guaranteed to contain at least one path to the destination.