gh-pages
krahets 2 years ago
parent 7752a20cf0
commit 7b7c69d192

@ -2901,7 +2901,29 @@
<a id="__codelineno-43-23" name="__codelineno-43-23" href="#__codelineno-43-23"></a><span class="w"> </span><span class="o">*</span><span class="nx">state</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="nx">state</span><span class="p">)[:</span><span class="nb">len</span><span class="p">(</span><span class="o">*</span><span class="nx">state</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<a id="__codelineno-43-24" name="__codelineno-43-24" href="#__codelineno-43-24"></a><span class="p">}</span>
<a id="__codelineno-43-25" name="__codelineno-43-25" href="#__codelineno-43-25"></a>
<a id="__codelineno-43-26" name="__codelineno-43-26" href="#__codelineno-43-26"></a><span class="p">[</span><span class="nx">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="kd">func</span><span class="p">]{</span><span class="nx">backtrack</span><span class="p">}</span>
<a id="__codelineno-43-26" name="__codelineno-43-26" href="#__codelineno-43-26"></a><span class="cm">/* 回溯算法:例题三 */</span>
<a id="__codelineno-43-27" name="__codelineno-43-27" href="#__codelineno-43-27"></a><span class="kd">func</span><span class="w"> </span><span class="nx">backtrackIII</span><span class="p">(</span><span class="nx">state</span><span class="w"> </span><span class="o">*</span><span class="p">[]</span><span class="o">*</span><span class="nx">TreeNode</span><span class="p">,</span><span class="w"> </span><span class="nx">choices</span><span class="w"> </span><span class="o">*</span><span class="p">[]</span><span class="o">*</span><span class="nx">TreeNode</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="w"> </span><span class="o">*</span><span class="p">[][]</span><span class="o">*</span><span class="nx">TreeNode</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-43-28" name="__codelineno-43-28" href="#__codelineno-43-28"></a><span class="w"> </span><span class="c1">// 检查是否为解</span>
<a id="__codelineno-43-29" name="__codelineno-43-29" href="#__codelineno-43-29"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nx">isSolution</span><span class="p">(</span><span class="nx">state</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-43-30" name="__codelineno-43-30" href="#__codelineno-43-30"></a><span class="w"> </span><span class="c1">// 记录解</span>
<a id="__codelineno-43-31" name="__codelineno-43-31" href="#__codelineno-43-31"></a><span class="w"> </span><span class="nx">recordSolution</span><span class="p">(</span><span class="nx">state</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">)</span>
<a id="__codelineno-43-32" name="__codelineno-43-32" href="#__codelineno-43-32"></a><span class="w"> </span><span class="k">return</span>
<a id="__codelineno-43-33" name="__codelineno-43-33" href="#__codelineno-43-33"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-43-34" name="__codelineno-43-34" href="#__codelineno-43-34"></a><span class="w"> </span><span class="c1">// 遍历所有选择</span>
<a id="__codelineno-43-35" name="__codelineno-43-35" href="#__codelineno-43-35"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">choice</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="o">*</span><span class="nx">choices</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-43-36" name="__codelineno-43-36" href="#__codelineno-43-36"></a><span class="w"> </span><span class="c1">// 剪枝:检查选择是否合法</span>
<a id="__codelineno-43-37" name="__codelineno-43-37" href="#__codelineno-43-37"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nx">isValid</span><span class="p">(</span><span class="nx">state</span><span class="p">,</span><span class="w"> </span><span class="nx">choice</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-43-38" name="__codelineno-43-38" href="#__codelineno-43-38"></a><span class="w"> </span><span class="c1">// 尝试:做出选择,更新状态</span>
<a id="__codelineno-43-39" name="__codelineno-43-39" href="#__codelineno-43-39"></a><span class="w"> </span><span class="nx">makeChoice</span><span class="p">(</span><span class="nx">state</span><span class="p">,</span><span class="w"> </span><span class="nx">choice</span><span class="p">)</span>
<a id="__codelineno-43-40" name="__codelineno-43-40" href="#__codelineno-43-40"></a><span class="w"> </span><span class="c1">// 进行下一轮选择</span>
<a id="__codelineno-43-41" name="__codelineno-43-41" href="#__codelineno-43-41"></a><span class="w"> </span><span class="nx">temp</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">([]</span><span class="o">*</span><span class="nx">TreeNode</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
<a id="__codelineno-43-42" name="__codelineno-43-42" href="#__codelineno-43-42"></a><span class="w"> </span><span class="nx">temp</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">append</span><span class="p">(</span><span class="nx">temp</span><span class="p">,</span><span class="w"> </span><span class="nx">choice</span><span class="p">.</span><span class="nx">Left</span><span class="p">,</span><span class="w"> </span><span class="nx">choice</span><span class="p">.</span><span class="nx">Right</span><span class="p">)</span>
<a id="__codelineno-43-43" name="__codelineno-43-43" href="#__codelineno-43-43"></a><span class="w"> </span><span class="nx">backtrackIII</span><span class="p">(</span><span class="nx">state</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="nx">temp</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">)</span>
<a id="__codelineno-43-44" name="__codelineno-43-44" href="#__codelineno-43-44"></a><span class="w"> </span><span class="c1">// 回退:撤销选择,恢复到之前的状态</span>
<a id="__codelineno-43-45" name="__codelineno-43-45" href="#__codelineno-43-45"></a><span class="w"> </span><span class="nx">undoChoice</span><span class="p">(</span><span class="nx">state</span><span class="p">,</span><span class="w"> </span><span class="nx">choice</span><span class="p">)</span>
<a id="__codelineno-43-46" name="__codelineno-43-46" href="#__codelineno-43-46"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-43-47" name="__codelineno-43-47" href="#__codelineno-43-47"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-43-48" name="__codelineno-43-48" href="#__codelineno-43-48"></a><span class="p">}</span>
</code></pre></div>
</div>
<div class="tabbed-block">

File diff suppressed because one or more lines are too long

Binary file not shown.
Loading…
Cancel
Save