河内塔拼图模拟(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/towers-of-hanoi-puzzle-simulation-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 3 分钟阅读 - 1496 个词 阅读量 0河内塔拼图模拟(译文)
原文地址:https://www.codeproject.com/Articles/14777/Towers-of-Hanoi-Puzzle-Simulation
原文作者:Amr Elsehemy ®
译文由本站 robot-v1.0 翻译
前言
The article describes a recursive solution for the famous puzzle of the towers of Hanoi
本文介绍了著名的河内塔之谜的递归解决方案
介绍(Introduction)
的(The)河内塔(Tower of Hanoi)要么(or)河内塔(Towers of Hanoi)是一个数学游戏或难题.它由三个支架和许多大小不同的板组成,可以在任何支架上相互叠放.(is a mathematical game or puzzle. It consists of three stands, and a number of plates of different sizes which can be put over each other on any stand.) 难题始于将板按大小顺序堆叠在一个支架上,我们称其为源,顶部最小,形成金字塔形状.(The puzzle starts with the plates stacked in order of size on one stand we call source, smallest at the top, making a pyramidshape.) 游戏的目的是遵守以下规则将整个堆栈移动到另一个位置(目标):(The object of the game is to move the entire stack to another stand (destination), obeying the following rules:)
- 一次只能移动一张光盘.(Only one disc may be moved at a time.)
- 请勿将任何光盘放置在较小的光盘上.(No disc may be placed on top of a smaller disc.)
解(Solution)
递归算法(Recursive Algorithm)
-
标记展台Src,Intr和目的地.(Label the stands Src, Intr, Dest.)
-
让(Let)**ñ(n)**是光盘的总数.(be the total number of discs.)
-
将光盘从1(最小,最上面)编号到(Number the discs from 1 (smallest, topmost) to)ñ(n)(最大,最底端).((largest, bottommost).) 要将光盘从Src支架移动到Desk支架,请执行以下操作:(To move n discs from stand Src to stand Dest:)
-
将n-1个板从Src移至Intr.这将板#n留在板Src上.(Move n-1 plates from Src to Intr. This leaves plate #n alone on plate Src.)
-
将板#n从Src移至目的地.(Move plate #n from Src to Dest.)
-
将n-1个板块从Intr移至Dest,以使它们位于#n板上.(Move n-1 plates from Intr to Dest so they sit on plate #n.) 上面是递归算法:要执行第1步和第3步,请针对以下内容再次应用相同的算法(The above is a recursive algorithm: to carry out steps 1 and 3, apply the same algorithm again for)ñ(n)-1.整个过程是有限数量的步骤,因为在某些时候将需要算法(-1. The entire procedure is a finite number of steps, since at some point the algorithm will be required for)ñ(n)=1.从支架Src到支架Dest移动单个板的步骤很简单.(= 1. This step, moving a single plate from stand Src to stand Dest, is trivial.) 河内之塔是一个经常用于教授入门编程的问题,特别是作为简单递归算法的示例.它也是指数时间算法的一个示例-对于除最小数量的光盘以外的所有光盘,即使在世界上最快的计算机上,也将花费不切实际的大量时间.无法对此进行改进,因为解决难题所需的最小移动数与板数成指数关系,即2(The Tower of Hanoi is a problem often used to teach beginning programming, in particular as an example of a simple recursive algorithm. It is also an example of an exponential time algorithm — for all but the smallest number of discs, it will take an impractically huge amount of time, even on the fastest computers in the world. There is no way to improve on this, because the minimum number of moves required to solve the puzzle is exponential in the number of plates, which is 2)ñ(n)-1,使用递归关系计算,其中(- 1, calculated by using recurrence relations, where)**ñ(n)**是板数.(is the number of plates.)
码(Code)
SolveTowers()(SolveTowers())
// code for the recursive method
// count are number of plates,
// source stand, destination stand and intermediate stand
private void SolveTowers(int count, int source, int dest, int inter)
{
if (count == 1)
{
MoveFromTo(source, dest);//To Draw the action performed
TotalMoves++; //keep track of number of moves
}
else
{
//1- Move n-1 from source to intermediate stand using destination as a
//spare
solveTowers(count - 1, source, inter, dest);
//2-Move plate #n from Src to Dest
solveTowers(1, source, dest, inter);
//3-Move n-1 plates from intermediate to dest so they sit on plate #n
solveTowers(count - 1, inter, dest, source);
}
}
参考(Reference)
历史(History)
- 12(12)日(th)2006年7月:初始职位(July, 2006: Initial post)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# .NET Windows Visual-Studio Dev 新闻 翻译