Walking the Labyrinth
Given an n × m two-dimensional integer array representing a maze, the array contains only 0 or 1, where 0 indicates a passable path and 1 indicates an impassable wall.
Initially, a person is located at the top-left corner (1, 1), and it is known that the person can move one position in any of the four directions: up, down, left, or right, each time.
Please determine the minimum number of moves required for the person to move from the top-left corner to the bottom-right corner (n, m).
The data guarantees that the numbers at (1, 1) and (n, m) are 0.
Input format
The first line contains two integers n and m.
The next n lines each contain m integers (0 or 1), representing the complete two-dimensional array maze.
Output format
Output an integer, representing the minimum number of moves from the top-left corner to the bottom-right corner, output -1 if there is no path.
Data range
1 ≤ n, m ≤ 100
Sample input
5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
Sample output
8
Comments