Problem:

There are three poles and N disks where each disk is heaver than the next disk. In the initial conguration, the discs are stacked upon another on the first pole where the lighter discs are above the heavier discs. We want to move all the discs to the last pole with the following conditions:

  • Only one disc can be moved from one pole to another at a time.
  • The discs have to be stacked such that all the lighter discs are on top of the heavier ones.

Recursion:

To move N discs from the first pole to the last pole, we need to move N-1 discs to the middle pole, then move the Nth disc to the last pole, and then move all N-1 discs from the middle pole back to the last pole.

Code:

#include <iostream>

using namespace std;

void hanoi(int N,int start,int helper,int destination){
if(N==1)
cout<<"Move "<<start<<" to "<<destination<<endl;
else{
hanoi(N-1,start,destination,helper);
hanoi(1,start,helper,destination);
hanoi(N-1,helper,start,destination);
}
} int main()
{
int N=10;
hanoi(10,1,2,3);
return 0;
}

(算法)Hanoi Problem汉诺塔问题的更多相关文章

  1. JavaScript算法实现之汉诺塔(Hanoi)

    目前前端新手,看到的不喜勿喷,还望大神指教. 随着Node.js,Angular.js,JQuery的流行,点燃了我学习JavaScript的热情!以后打算每天早上跟晚上抽2小时左右时间将经典的算法都 ...

  2. 算法笔记_013:汉诺塔问题(Java递归法和非递归法)

    目录 1 问题描述 2 解决方案  2.1 递归法 2.2 非递归法 1 问题描述 Simulate the movement of the Towers of Hanoi Puzzle; Bonus ...

  3. 《hanoi(汉诺塔)问题》求解

    //Hanoi(汉诺)塔问题.这是一个古典的数学问题,用递归方法求解.问题如下: /* 古代有一个梵塔,塔内有3个座A,B,C,开始时A座上有64个盘子,盘子大小不等,大的在下,小的在上. 有一个老和 ...

  4. C语言之算法初步(汉诺塔--递归算法)

    个人觉得汉诺塔这个递归算法比电子老鼠的难了一些,不过一旦理解了也还是可以的,其实网上也有很多代码,可以直接参考.记得大一开始时就做过汉诺塔的习题,但是那时代码写得很长很长,也是不理解递归的结果.现在想 ...

  5. hanoi(汉诺塔)递归实现

    汉诺塔:汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具.大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘.大梵天命令婆罗门把圆盘从下面开始按大小顺序 ...

  6. Hanoi II——汉诺塔步数求解进阶问题

    在NOJ上遇到关于汉诺塔步数的求解问题 开始读时一脸懵逼,甚至不知道输入的数据是什么意思 题目描述:给出汉诺塔的两个状态,从初始状态移动到目的状态所需要的最少步数 对于初级汉诺塔步数问题,我们可以直接 ...

  7. Python算法_递归:汉诺塔

    游戏链接:https://zhangxiaoleiv.github.io/app/TowerOfHanoi/Hanoi.html 汉诺塔游戏算法: 1 def hanoi(n,x,y,z): 2 if ...

  8. Java-Runoob-高级教程-实例-方法:03. Java 实例 – 汉诺塔算法-un

    ylbtech-Java-Runoob-高级教程-实例-方法:03. Java 实例 – 汉诺塔算法 1.返回顶部 1. Java 实例 - 汉诺塔算法  Java 实例 汉诺塔(又称河内塔)问题是源 ...

  9. 几年前做家教写的C教程(之四专讲了指针与汉诺塔问题)

    C语言学习宝典(4) 指针:可以有效的表示复杂的数据结构,能动态的分配动态空间,方便的使用字符串,有效的使用数组,能直接处理内存单元 不掌握指针就没有掌握C语言的精华 地址:系统为每一个变量分配一个内 ...

随机推荐

  1. Python的环境搭建——万丈高楼平地起

    Python的环境搭建,远程连接,端口映射,虚拟机 写在正文之前 python语言的开发环境还是相对比较简单的,但是也是有很多需要注意的地方,对于初次接触python或者以前很少用到虚拟环境的朋友来说 ...

  2. DateFormat 线程安全

    SimpleDateformat 线程不安全 SimpleDateFormat 继承自 DateFormat, SimpleDateFormat中的parse方法override父类DateForma ...

  3. 洗牌问题 FZU - 1062 (传说中的思路题,hhh)

    设2n张牌分别标记为1, 2, -, n, n+1, -, 2n,初始时这2n张牌按其标号从小到大排列.经一次洗牌后,原来的排列顺序变成n+1, 1, n+2, 2, -, 2n, n.即前n张牌被放 ...

  4. FastReport.Net使用:[25]除数0处理方法

    使用系统函数IIF判断处理 1.IIF函数介绍 public static Object IIf( bool expression, Object truePart, Object falsePart ...

  5. 多个Fragment在屏幕翻转会重影问题的解决

    fragment使用add和hide而不用replace的方法添加到activity中,如果屏幕翻转可能会又add新的fragment进去,所以会重影. 如果有一个sparsearray保存fragm ...

  6. --whole-archive和--no-whole-archive

    --whole-archive选项解决的是编译中常遇到的问题.在代码中定义的符号(如函数名)还未使用到之前,链接器并不会把它加入到连接表中. 如下面这个例子: a.cpp: void func(){p ...

  7. bzoj 2179: FFT快速傅立叶 -- FFT

    2179: FFT快速傅立叶 Time Limit: 10 Sec  Memory Limit: 259 MB Description 给出两个n位10进制整数x和y,你需要计算x*y. Input ...

  8. 中国剩余定理 hdu 1573 X问题

    HDU 1573 X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. 查看linux启动的线程信息

    1.某一进程所有的线程个数.启动时间 ps max -o lstart,lwp,pid,nlwp,cmd|more lstart:     STARTED time the command start ...

  10. 用Javascript轻松制作一套简单的抽奖系统

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN"> <html> <head ...