Loading...
Loading...
You are given a two-dimensional integer array matrix representing a maze. Each cell is one of:
0: an empty cell1: a wallYou start at matrix[0][0] and want to reach matrix[n - 1][m - 1], where n and m are the number of rows and columns in matrix. Each step moves to a horizontally or vertically adjacent empty cell.
Return the minimum number of steps needed to reach the bottom-right cell. If the maze has only one cell, 0 steps are needed. If the top-left cell, the bottom-right cell, or every path between them is blocked, return -1.
Input: matrix = [[0, 0, 0],
[1, 1, 0],
[0, 0, 0]]
Output: 4
Explanation: One shortest route is (0,0), (0,1), (0,2), (1,2), (2,2), which takes 4 steps.
Input: matrix = [[0, 1],
[1, 0]]
Output: -1
Explanation: The top-left and bottom-right cells are diagonal from each other and every direct neighbor is a wall, so there is no path.
Input: matrix = [[0]]
Output: 0
matrix.lengthmatrix[0].lengthmatrix[i][j] is 0 or 1.