Skip to content

Commit

Permalink
Update 0542. 01 矩阵.md
Browse files Browse the repository at this point in the history
  • Loading branch information
itcharge committed Jan 9, 2022
1 parent 08f54ff commit 0655d37
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Solutions/0542. 01 矩阵.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

## 题目大意

给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离。
给定一个由 `0``1` 组成的矩阵。

要求:找出每个元素到最近的 `0` 的距离。

## 解题思路

题目要求的是每个 `1``0`的最短曼哈顿距离。换句话也可以求每个 `0``1` 的最短曼哈顿距离。这样做的好处是,可以从所有值为 `0` 的元素开始进行搜索,可以不断累积距离,直到遇到值为 `1` 的元素时,可以直接将累积距离直接赋值。

具体操作如下:将所有值为 `0` 的元素坐标加入访问集合中,对所有值为`0` 的元素上下左右进行搜索。每进行一次上下左右搜索,更新新位置的距离值,并把新的位置坐标加入队列和访问集合中,直到遇见值为 `1` 的元素停止搜索。
具体操作如下:将所有值为 `0` 的元素坐标加入访问集合中,对所有值为`0` 的元素上下左右进行广度优先搜索。每进行一次上下左右搜索,更新新位置的距离值,并把新的位置坐标加入队列和访问集合中,直到遇见值为 `1` 的元素停止搜索。

## 代码

Expand Down

0 comments on commit 0655d37

Please sign in to comment.