One usage of recurison: the tower of Hanoi

Statements: This blog was written by me, but most of content  is quoted from book【Data Structure with Java Hubbard】

【Description】

we have seen important examples of functions that are more naturally defined and more easily understood by using recursion. Foe some problem, recursion is the only reasonable method of solution.The towers of hanoi puzzle is a classical example of a problem
whose solution demands recursion. The game consists of a board with three vertical pegs labeled A, B, and C, and a sequence of n disks with holes in their centers. The radii of the disks are in an arithmetic progression(eg,6cm, 7cm, 8cm); and are mounted on
peg A. The rule is that no disk may be above a smaller disk on the same peg. The objective of the game is to move all the disks from peg A to peg C, one disk at a time, without violating the rule.

【Implement】

The program prints the solution to the towers of Hanoi problem of moving three disk from peg A to peg C via Peg B.

package com.albertshao.ds.recursion;

//  Data Structures with Java, Second Edition
//  by John R. Hubbard
//  Copyright 2007 by McGraw-Hill

public class TestHanoiTowers {
  public static void main(String[] args) {
    HanoiTowers(3, 'A', 'B', 'C');
  }

  public static void HanoiTowers(int n, char x, char y, char z) {
    if (n==1) {                // basis
      System.out.printf("Move top disk from peg %c to peg %c.%n", x, z);
    } else {
      HanoiTowers(n-1, x, z, y);  // recursion
      HanoiTowers(1, x, y, z);    // recursion
      HanoiTowers(n-1, y, x, z);  // recursion
    }
  }
}

【Result】

Move top disk from peg A to peg C.
Move top disk from peg A to peg B.
Move top disk from peg C to peg B.
Move top disk from peg A to peg C.
Move top disk from peg B to peg A.
Move top disk from peg B to peg C.
Move top disk from peg A to peg C.

One usage of recurison: the tower of Hanoi,布布扣,bubuko.com

时间: 2024-10-10 22:09:06

One usage of recurison: the tower of Hanoi的相关文章

poj 3601 Tower of Hanoi

Tower of Hanoi Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 1853   Accepted: 635 Description The Tower of Hanoi is a puzzle consisting of three pegs and a number of disks of different sizes which can slide onto any peg. The puzzle st

汉诺塔(Tower of Hanoi)问题的求解——利用栈与递归

汉诺塔(Tower of Hanoi)问题的求解--利用栈与递归 1. 汉诺塔问题的提法 汉诺塔问题是使用递归解决问题的经典范例. 传说婆罗门庙里有一个塔台,台上有3根标号为A.B.C的用钻石做成的柱子,在A柱上放着64个金盘,每一个都比下面的略小一点.把A柱上的金盘全部移到C柱上的那一天就是世界末日. 移动的条件是:一次只能移动一个金盘,移动过程中大金盘不能放在小金盘上面.庙里的僧人一直在移个不停,移动的最少总次数是264?1次,如果每秒移动一次的话,需要500亿年. 2. 求解汉诺塔问题的算

汉诺塔问题(The Tower of Hanoi)的递归算法与非递归算法

非递归算法: 根据圆盘的数量确定柱子的排放顺序: 若n为偶数,按顺时针方向依次摆放 A B C: 若n为奇数,按顺时针方向依次摆放 A C B. 然后进行如下操作: (1)按顺时针方向把圆盘1从现在的柱子移动到下一根柱子,即当n为偶数时,若圆盘1在柱子A,则把它移动到B:若圆盘1在柱子B,则把它移动到C:若圆盘1在柱子C,则把它移动到A. (2)接着,把另外两根柱子上可以移动的圆盘移动到新的柱子上.即把非空柱子上的圆盘移动到空柱子上,当两根柱子都非空时,移动较小的圆盘. (3)反复进行(1)(2

Tower of Hanoi问题

[问题描述] 有A, B, C三个塔座,A上套有n个直径不同的圆 盘,按直径从小到大叠放,形如宝塔,编号1, 2, 3 … n. 要求将n个圆盘从A移到C,叠放顺序不变,移动过程中遵循 下列原则: w每次只能移一个圆盘 w圆盘可在三个塔座上任意移动 w任何时刻,每个塔座上不能将大盘压到小盘上 [解决方法] n=1时,直接把圆盘从A移到C n>1时,先把上面n-1个圆盘从A移到B,然后将n号盘从A移到C,再将n-1个盘从B移到C.即把求解n个圆盘的Hanoi问题转化为求解n-1个圆盘的Hanoi问

汉诺塔 Tower of Hanoi

如果柱子标为A,B,C,要由A搬至C,在只有一个盘子时,就将它直接搬至C:当有两个盘子,就将B作为辅助柱:如果盘数超过2个,将第二个以下的盘子遮起来,就很简单了,每次处理两个盘子,也就是:A->B.A->C.B->C这三个步骤,而被遮起来的部分,其实就由方程的递归处理. 代码如下: #include <stdio.h> void hanoi(int n,char A,char B,char C){ if(n == 1){ printf("Move sheet %d

列表形式的汉诺塔(Tower of Hanoi)Python语言实现

从昨天半下午就开始想这个问题,到现在经过30个小时左右(期间并不思考是非常集中,因为连续思考很累而且可能效果不佳),终于把程序搞定了,第一次思考问题这么久.算是第一个自主思考的程序还是很有成就感的. 代码优势:模块化做的很好,找到了通过写出前4-5次的数学表达,找到了规律并将其化成代码. 代码劣势:规则化欠佳,因python无指针(网上说的,还不确定),没有学到python中类似c语言指针的函数,造成本来一个模块不得不分解为6个模块相互调用.并且函数封装不好,即因为不会类指针方法,所以用函数操作

URAL 2029 Towers of Hanoi Strike Back 汉诺塔,从初始状态到任意给出状态需要的次数

F - Towers of Hanoi Strike Back Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 2029 Description The Tower of Hanoi puzzle was invented by French mathematician édouard Lucas in the second half

poj 3601Tower of Hanoi

Tower of Hanoi Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 1895   Accepted: 646 Description The Tower of Hanoi is a puzzle consisting of three pegs and a number of disks of different sizes which can slide onto any peg. The puzzle st

poj 1958 Strange Towers of Hanoi

Strange Towers of Hanoi Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2678   Accepted: 1742 Description Background Charlie Darkbrown sits in another one of those boring Computer Science lessons: At the moment the teacher just explains