Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Shortest Clear Path | Solving the Problems using BFS
Breadth First Search
course content

Contenido del Curso

Breadth First Search

Breadth First Search

1. What is BFS
2. Practice
3. Improve Your Code
4. Solving the Problems using BFS

Shortest Clear Path

Tarea

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.

Tarea

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

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 4. Capítulo 2
toggle bottom row

Shortest Clear Path

Tarea

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.

Tarea

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

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 4. Capítulo 2
toggle bottom row

Shortest Clear Path

Tarea

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.

Tarea

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

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Tarea

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

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 4. Capítulo 2
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt