1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/二进制矩阵中的最短路径 [shortest-path-in-binary-matrix].html
2022-03-29 12:43:11 +08:00

45 lines
1.7 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个 <code>n x n</code> 的二进制矩阵 <code>grid</code> 中,返回矩阵中最短 <strong>畅通路径</strong> 的长度。如果不存在这样的路径,返回 <code>-1</code></p>
<p>二进制矩阵中的 畅通路径 是一条从 <strong>左上角</strong> 单元格(即,<code>(0, 0)</code>)到 右下角 单元格(即,<code>(n - 1, n - 1)</code>)的路径,该路径同时满足下述要求:</p>
<ul>
<li>路径途经的所有单元格都的值都是 <code>0</code></li>
<li>路径中所有相邻的单元格应当在 <strong>8 个方向之一</strong> 上连通(即,相邻两单元之间彼此不同且共享一条边或者一个角)。</li>
</ul>
<p><strong>畅通路径的长度</strong> 是该路径途经的单元格总数。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/example1_1.png" style="width: 500px; height: 234px;" />
<pre>
<strong>输入:</strong>grid = [[0,1],[1,0]]
<strong>输出:</strong>2
</pre>
<p><strong>示例 2</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/example2_1.png" style="height: 216px; width: 500px;" />
<pre>
<strong>输入:</strong>grid = [[0,0,0],[1,1,0],[1,1,0]]
<strong>输出:</strong>4
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>grid = [[1,0,0],[1,1,0],[1,1,0]]
<strong>输出:</strong>-1
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>grid[i][j]</code><code>0</code><code>1</code></li>
</ul>