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)/网格图中最少访问的格子数 [minimum-number-of-visited-cells-in-a-grid].html

53 lines
2.2 KiB
HTML
Raw Normal View History

2023-04-14 14:39:57 +08:00
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的&nbsp;<code>m x n</code>&nbsp;整数矩阵&nbsp;<code>grid</code>&nbsp;。你一开始的位置在&nbsp;<strong>左上角</strong>&nbsp;格子&nbsp;<code>(0, 0)</code>&nbsp;</p>
<p>当你在格子&nbsp;<code>(i, j)</code>&nbsp;的时候,你可以移动到以下格子之一:</p>
<ul>
<li>满足 <code>j &lt; k &lt;= grid[i][j] + j</code>&nbsp;的格子&nbsp;<code>(i, k)</code>&nbsp;(向右移动),或者</li>
<li>满足 <code>i &lt; k &lt;= grid[i][j] + i</code>&nbsp;的格子&nbsp;<code>(k, j)</code>&nbsp;(向下移动)。</li>
</ul>
<p>请你返回到达 <strong>右下角</strong>&nbsp;格子&nbsp;<code>(m - 1, n - 1)</code>&nbsp;需要经过的最少移动格子数,如果无法到达右下角格子,请你返回&nbsp;<code>-1</code>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex1.png" style="width: 271px; height: 171px;"></p>
<pre><b>输入:</b>grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]
<b>输出:</b>4
<b>解释:</b>上图展示了到达右下角格子经过的 4 个格子。
</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex2.png" style="width: 271px; height: 171px;"></p>
<pre><b>输入:</b>grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]
<b>输出:</b>3
<strong>解释:</strong>上图展示了到达右下角格子经过的 3 个格子。
</pre>
<p><strong>示例 3</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/01/26/ex3.png" style="width: 181px; height: 81px;"></p>
<pre><b>输入:</b>grid = [[2,1,0],[1,0,0]]
<b>输出:</b>-1
<b>解释:</b>无法到达右下角格子。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= grid[i][j] &lt; m * n</code></li>
<li><code>grid[m - 1][n - 1] == 0</code></li>
</ul>