|
6 | 6 |
|
7 | 7 | <!-- 这里写题目描述 -->
|
8 | 8 |
|
9 |
| -<p>现在有一棵树,一只松鼠和一些坚果。位置由二维网格的单元格表示。你的目标是找到松鼠收集所有坚果的<strong>最小路程</strong>,且坚果是一颗接一颗地被放在树下。松鼠一次最多只能携带<strong>一颗</strong>坚果,松鼠可以向上,向下,向左和向右四个方向移动到相邻的单元格。移动次数表示路程。</p> |
10 |
| - |
11 |
| -<p><strong>输入 1:</strong></p> |
12 |
| - |
13 |
| -<pre><strong>输入:</strong> |
14 |
| -高度 : 5 |
15 |
| -宽度 : 7 |
16 |
| -树的位置 : [2,2] |
17 |
| -松鼠 : [4,4] |
18 |
| -坚果 : [[3,0], [2,5]] |
19 |
| -<strong>输出:</strong> 12 |
20 |
| -<strong>解释:</strong> |
21 |
| -<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0500-0599/0573.Squirrel%20Simulation/images/squirrel_simulation.png" style="width: 40%;"> |
| 9 | +<p>给你两个整数 <code>height</code> 和 <code>width</code> ,代表一个大小为 <code>height x width</code> 的花园。你还得到了以下信息:</p> |
| 10 | + |
| 11 | +<ul> |
| 12 | + <li>一个数组 <code>tree</code> ,其中 <code>tree = [tree<sub>r</sub>, tree<sub>c</sub>]</code> 是花园中树的位置,</li> |
| 13 | + <li>一个数组 <code>squirrel</code> ,其中 <code>squirrel = [squirrel<sub>r</sub>, squirrel<sub>c</sub>]</code> 是花园中松鼠的位置,</li> |
| 14 | + <li>一个数组 <code>nuts</code> ,其中 <code>nuts[i] = [nut<sub>i<sub>r</sub></sub>, nut<sub>i<sub>c</sub></sub>]</code> 是花园中第 <code>i<sup>th</sup></code> 个坚果的位置。</li> |
| 15 | +</ul> |
| 16 | + |
| 17 | +<p>松鼠一次最多只能携带一个坚果,并且能够向上、下、左、右四个方向移动到相邻的单元格。</p> |
| 18 | + |
| 19 | +<p>返回松鼠收集所有坚果并逐一放在树下的 <strong>最小距离</strong> 。</p> |
| 20 | + |
| 21 | +<p><strong>距离</strong> 是指移动的次数。</p> |
| 22 | + |
| 23 | +<p> </p> |
| 24 | + |
| 25 | +<p><strong class="example">示例 1:</strong></p> |
| 26 | +<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0500-0599/0573.Squirrel%20Simulation/images/squirrel1-grid.jpg" style="width: 573px; height: 413px;" /> |
| 27 | +<pre> |
| 28 | +<strong>输入:</strong>height = 5, width = 7, tree = [2,2], squirrel = [4,4], nuts = [[3,0], [2,5]] |
| 29 | +<strong>输出:</strong>12 |
| 30 | +<strong>解释:</strong>为实现最小的距离,松鼠应该先摘 [2, 5] 位置的坚果。 |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><strong class="example">示例 2:</strong></p> |
| 34 | +<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0500-0599/0573.Squirrel%20Simulation/images/squirrel2-grid.jpg" style="width: 253px; height: 93px;" /> |
| 35 | +<pre> |
| 36 | +<strong>输入:</strong>height = 1, width = 3, tree = [0,1], squirrel = [0,0], nuts = [[0,2]] |
| 37 | +<strong>输出:</strong>3 |
22 | 38 | </pre>
|
23 | 39 |
|
24 |
| -<p><strong>注意:</strong></p> |
| 40 | +<p> </p> |
| 41 | + |
| 42 | +<p><strong>提示:</strong></p> |
25 | 43 |
|
26 |
| -<ol> |
27 |
| - <li>所有给定的位置不会重叠。</li> |
28 |
| - <li>松鼠一次最多只能携带一颗坚果。</li> |
29 |
| - <li>给定的坚果位置没有顺序。</li> |
30 |
| - <li>高度和宽度是正整数。 3 <= 高度 * 宽度 <= 10,000。</li> |
31 |
| - <li>给定的网格至少包含一颗坚果,唯一的一棵树和一只松鼠。</li> |
32 |
| -</ol> |
| 44 | +<ul> |
| 45 | + <li><code>1 <= height, width <= 100</code></li> |
| 46 | + <li><code>tree.length == 2</code></li> |
| 47 | + <li><code>squirrel.length == 2</code></li> |
| 48 | + <li><code>1 <= nuts.length <= 5000</code></li> |
| 49 | + <li><code>nuts[i].length == 2</code></li> |
| 50 | + <li><code>0 <= tree<sub>r</sub>, squirrel<sub>r</sub>, nut<sub>i<sub>r</sub></sub> <= height</code></li> |
| 51 | + <li><code>0 <= tree<sub>c</sub>, squirrel<sub>c</sub>, nut<sub>i<sub>c</sub></sub> <= width</code></li> |
| 52 | +</ul> |
33 | 53 |
|
34 | 54 | ## 解法
|
35 | 55 |
|
|
0 commit comments