mirror of
https://github.com/krahets/hello-algo.git
synced 2025-08-02 19:53:11 +08:00
deploy
This commit is contained in:
@ -3449,7 +3449,7 @@
|
||||
|
||||
<p><strong>我们将规模为 <span class="arithmatex">\(i\)</span> 的汉诺塔问题记做 <span class="arithmatex">\(f(i)\)</span></strong> 。例如 <span class="arithmatex">\(f(3)\)</span> 代表将 <span class="arithmatex">\(3\)</span> 个圆盘从 <code>A</code> 移动至 <code>C</code> 的汉诺塔问题。</p>
|
||||
<h3 id="1">1. 考虑基本情况<a class="headerlink" href="#1" title="Permanent link">¶</a></h3>
|
||||
<p>对于问题 <span class="arithmatex">\(f(1)\)</span> ,即当只有一个圆盘时,则将它直接从 <code>A</code> 移动至 <code>C</code> 即可。</p>
|
||||
<p>如下图所示,对于问题 <span class="arithmatex">\(f(1)\)</span> ,即当只有一个圆盘时,我们将它直接从 <code>A</code> 移动至 <code>C</code> 即可。</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="1:2"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1"><1></label><label for="__tabbed_1_2"><2></label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
@ -3462,13 +3462,12 @@
|
||||
</div>
|
||||
<p align="center"> 图:规模为 1 问题的解 </p>
|
||||
|
||||
<p>对于问题 <span class="arithmatex">\(f(2)\)</span> ,即当有两个圆盘时,<strong>由于要时刻满足小圆盘在大圆盘之上,因此需要借助 <code>B</code> 来完成移动</strong>,包括三步:</p>
|
||||
<p>如下图所示,对于问题 <span class="arithmatex">\(f(2)\)</span> ,即当有两个圆盘时,<strong>由于要时刻满足小圆盘在大圆盘之上,因此需要借助 <code>B</code> 来完成移动</strong>。</p>
|
||||
<ol>
|
||||
<li>先将上面的小圆盘从 <code>A</code> 移至 <code>B</code> 。</li>
|
||||
<li>再将大圆盘从 <code>A</code> 移至 <code>C</code> 。</li>
|
||||
<li>最后将小圆盘从 <code>B</code> 移至 <code>C</code> 。</li>
|
||||
</ol>
|
||||
<p>解决问题 <span class="arithmatex">\(f(2)\)</span> 的过程可总结为:<strong>将两个圆盘借助 <code>B</code> 从 <code>A</code> 移至 <code>C</code></strong> 。其中,<code>C</code> 称为目标柱、<code>B</code> 称为缓冲柱。</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="2:4"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1"><1></label><label for="__tabbed_2_2"><2></label><label for="__tabbed_2_3"><3></label><label for="__tabbed_2_4"><4></label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
@ -3487,14 +3486,15 @@
|
||||
</div>
|
||||
<p align="center"> 图:规模为 2 问题的解 </p>
|
||||
|
||||
<p>解决问题 <span class="arithmatex">\(f(2)\)</span> 的过程可总结为:<strong>将两个圆盘借助 <code>B</code> 从 <code>A</code> 移至 <code>C</code></strong> 。其中,<code>C</code> 称为目标柱、<code>B</code> 称为缓冲柱。</p>
|
||||
<h3 id="2">2. 子问题分解<a class="headerlink" href="#2" title="Permanent link">¶</a></h3>
|
||||
<p>对于问题 <span class="arithmatex">\(f(3)\)</span> ,即当有三个圆盘时,情况变得稍微复杂了一些。由于已知 <span class="arithmatex">\(f(1)\)</span> 和 <span class="arithmatex">\(f(2)\)</span> 的解,因此可从分治角度思考,<strong>将 <code>A</code> 顶部的两个圆盘看做一个整体</strong>,执行以下步骤:</p>
|
||||
<p>对于问题 <span class="arithmatex">\(f(3)\)</span> ,即当有三个圆盘时,情况变得稍微复杂了一些。</p>
|
||||
<p>因为已知 <span class="arithmatex">\(f(1)\)</span> 和 <span class="arithmatex">\(f(2)\)</span> 的解,所以我们可从分治角度思考,<strong>将 <code>A</code> 顶部的两个圆盘看做一个整体</strong>,执行下图所示的步骤。这样三个圆盘就被顺利地从 <code>A</code> 移动至 <code>C</code> 了。</p>
|
||||
<ol>
|
||||
<li>令 <code>B</code> 为目标柱、<code>C</code> 为缓冲柱,将两个圆盘从 <code>A</code> 移动至 <code>B</code> 。</li>
|
||||
<li>将 <code>A</code> 中剩余的一个圆盘从 <code>A</code> 直接移动至 <code>C</code> 。</li>
|
||||
<li>令 <code>C</code> 为目标柱、<code>A</code> 为缓冲柱,将两个圆盘从 <code>B</code> 移动至 <code>C</code> 。</li>
|
||||
</ol>
|
||||
<p>这样三个圆盘就被顺利地从 <code>A</code> 移动至 <code>C</code> 了。</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="3:4"><input checked="checked" id="__tabbed_3_1" name="__tabbed_3" type="radio" /><input id="__tabbed_3_2" name="__tabbed_3" type="radio" /><input id="__tabbed_3_3" name="__tabbed_3" type="radio" /><input id="__tabbed_3_4" name="__tabbed_3" type="radio" /><div class="tabbed-labels"><label for="__tabbed_3_1"><1></label><label for="__tabbed_3_2"><2></label><label for="__tabbed_3_3"><3></label><label for="__tabbed_3_4"><4></label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
@ -3514,7 +3514,7 @@
|
||||
<p align="center"> 图:规模为 3 问题的解 </p>
|
||||
|
||||
<p>本质上看,<strong>我们将问题 <span class="arithmatex">\(f(3)\)</span> 划分为两个子问题 <span class="arithmatex">\(f(2)\)</span> 和子问题 <span class="arithmatex">\(f(1)\)</span></strong> 。按顺序解决这三个子问题之后,原问题随之得到解决。这说明子问题是独立的,而且解是可以合并的。</p>
|
||||
<p>至此,我们可总结出汉诺塔问题的分治策略:将原问题 <span class="arithmatex">\(f(n)\)</span> 划分为两个子问题 <span class="arithmatex">\(f(n-1)\)</span> 和一个子问题 <span class="arithmatex">\(f(1)\)</span> 。子问题的解决顺序为:</p>
|
||||
<p>至此,我们可总结出下图所示的汉诺塔问题的分治策略:将原问题 <span class="arithmatex">\(f(n)\)</span> 划分为两个子问题 <span class="arithmatex">\(f(n-1)\)</span> 和一个子问题 <span class="arithmatex">\(f(1)\)</span> 。子问题的解决顺序为:</p>
|
||||
<ol>
|
||||
<li>将 <span class="arithmatex">\(n-1\)</span> 个圆盘借助 <code>C</code> 从 <code>A</code> 移至 <code>B</code> 。</li>
|
||||
<li>将剩余 <span class="arithmatex">\(1\)</span> 个圆盘从 <code>A</code> 直接移至 <code>C</code> 。</li>
|
||||
|
Reference in New Issue
Block a user