1091. Shortest Path in Binary Matrix

1091. Shortest Path in Binary Matrix #

Problem #

In an N by N square grid, each cell is either empty (0) or blocked (1).

clear path from top-left to bottom-right has length k if and only if it is composed of cells C_1, C_2, ..., C_k such that:

  • Adjacent cells C_i and C_{i+1} are connected 8-directionally (ie., they are different and share an edge or corner)
  • C_1 is at location (0, 0) (ie. has value grid[0][0])
  • C_k is at location (N-1, N-1) (ie. has value grid[N-1][N-1])
  • If C_i is located at (r, c), then grid[r][c] is empty (ie. grid[r][c] == 0).

Return the length of the shortest such clear path from top-left to bottom-right.  If such a path does not exist, return -1.

Example 1:

Input: [[0,1],[1,0]]
Output: 2

https://assets.leetcode.com/uploads/2019/08/04/example1_1.png

https://assets.leetcode.com/uploads/2019/08/04/example1_2.png

Example 2:

Input: [[0,0,0],[1,1,0],[1,1,0]]
Output: 4

https://assets.leetcode.com/uploads/2019/08/04/example2_1.png

https://assets.leetcode.com/uploads/2019/08/04/example2_2.png

Note:

  1. 1 <= grid.length == grid[0].length <= 100
  2. grid[r][c] is 0 or 1

Problem Summary #

In an N × N square grid, each cell has two states: empty (0) or blocked (1). A clear path from the top-left to the bottom-right with length k is composed of cells C_1, C_2, …, C_k that satisfy the following conditions:

  • Adjacent cells C_i and C_{i+1} are connected in one of eight directions (at this point, C_i and C_{i+1} are different and share an edge or corner)
  • C_1 is located at (0, 0) (i.e., has value grid[0][0])
  • C_k is located at (N-1, N-1) (i.e., has value grid[N-1][N-1])
  • If C_i is located at (r, c), then grid[r][c] is empty (i.e., grid[r][c] == 0)

Return the length of the shortest clear path from the top-left to the bottom-right. If no such path exists, return -1.

Solution Approach #

  • This problem is a simple shortest path search. Using BFS to expand step by step from the top-left to the bottom-right makes it easy to solve. Note that each round of expansion needs to consider 8 directions.

Code #

var dir = [][]int{
	{-1, -1},
	{-1, 0},
	{-1, 1},
	{0, 1},
	{0, -1},
	{1, -1},
	{1, 0},
	{1, 1},
}

func shortestPathBinaryMatrix(grid [][]int) int {
	visited := make([][]bool, 0)
	for range make([]int, len(grid)) {
		visited = append(visited, make([]bool, len(grid[0])))
	}
	dis := make([][]int, 0)
	for range make([]int, len(grid)) {
		dis = append(dis, make([]int, len(grid[0])))
	}
	if grid[0][0] == 1 {
		return -1
	}
	if len(grid) == 1 && len(grid[0]) == 1 {
		return 1
	}

	queue := []int{0}
	visited[0][0], dis[0][0] = true, 1
	for len(queue) > 0 {
		cur := queue[0]
		queue = queue[1:]
		curx, cury := cur/len(grid[0]), cur%len(grid[0])
		for d := 0; d < 8; d++ {
			nextx := curx + dir[d][0]
			nexty := cury + dir[d][1]
			if isInBoard(grid, nextx, nexty) && !visited[nextx][nexty] && grid[nextx][nexty] == 0 {
				queue = append(queue, nextx*len(grid[0])+nexty)
				visited[nextx][nexty] = true
				dis[nextx][nexty] = dis[curx][cury] + 1
				if nextx == len(grid)-1 && nexty == len(grid[0])-1 {
					return dis[nextx][nexty]
				}
			}
		}
	}
	return -1
}

func isInBoard(board [][]int, x, y int) bool {
	return x >= 0 && x < len(board) && y >= 0 && y < len(board[0])
}

Calendar Jun 25, 2026
Edit Edit this page
Total visits:   You are visitor No.
中文