Shortest Clear Path
Swipe to start coding
Given an n x n matrix grid
, which contains of 0
and 1
only. Find the length of the shortest path in the matrix, which starts with grid[0][0]
and ends in grid[n-1][n-1]
, and next conditions are satisfied:
- All cell of the path are
0
- All the adjacent cells of the path are connected 4-directionally.
The length of such a path is number of visited cells.
If there is no clear path, return -1.
Example 1
Input: n=3, grid
= [[0, 0, 0],[1, 0 ,0],[1, 1, 0]]
Output: 5
Example 2
Input: n = 3, grid
= [[0,1,0],[0,0,1],[1,1,0]]
Output: -1
Tak for dine kommentarer!
single
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 7.69
Shortest Clear Path
Stryg for at vise menuen
Swipe to start coding
Given an n x n matrix grid
, which contains of 0
and 1
only. Find the length of the shortest path in the matrix, which starts with grid[0][0]
and ends in grid[n-1][n-1]
, and next conditions are satisfied:
- All cell of the path are
0
- All the adjacent cells of the path are connected 4-directionally.
The length of such a path is number of visited cells.
If there is no clear path, return -1.
Example 1
Input: n=3, grid
= [[0, 0, 0],[1, 0 ,0],[1, 1, 0]]
Output: 5
Example 2
Input: n = 3, grid
= [[0,1,0],[0,0,1],[1,1,0]]
Output: -1
Tak for dine kommentarer!
Awesome!
Completion rate improved to 7.69single