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.

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3hiMDg0MTkwMTExNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

【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的更多相关文章

  1. poj 3601 Tower of Hanoi

    Tower of Hanoi Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 1853   Accepted: 635 De ...

  2. python递归三战:Sierpinski Triangle、Tower of Hanoi、Maze Exploring

    本文已做成视频教程投稿b站(视频版相对文本版有一些改进),点击观看视频教程 本文主要通过三个实例来帮助大家理解递归(其展示动画已上传B站): 谢尔宾斯基三角形(Sierpinski Triangle) ...

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

    非递归算法: 根据圆盘的数量确定柱子的排放顺序: 若n为偶数,按顺时针方向依次摆放 A B C: 若n为奇数,按顺时针方向依次摆放 A C B. 然后进行如下操作: (1)按顺时针方向把圆盘1从现在的 ...

  4. Tower of Hanoi问题

    [问题描述] 有A, B, C三个塔座,A上套有n个直径不同的圆 盘,按直径从小到大叠放,形如宝塔,编号1, 2, 3 … n. 要求将n个圆盘从A移到C,叠放顺序不变,移动过程中遵循 下列原则: w ...

  5. [POJ1958][Strange Tower of Hanoi]

    题目描述 求解 \(n\) 个盘子 \(4\) 座塔的 Hanoi 问题最少需要多少步 问题分析 考虑 \(3\) 座塔的 Hanoi 问题,记 \(f[i]\) 表示最少需要多少步, 则 \(f[i ...

  6. 汉诺塔 Tower of Hanoi

    假设柱子标为A,B.C.要由A搬至C,在仅仅有一个盘子时,就将它直接搬至C:当有两个盘子,就将B作为辅助柱.假设盘数超过2个.将第二个下面的盘子遮起来,就非常easy了.每次处理两个盘子,也就是:A- ...

  7. codeforces 392B Tower of Hanoi

    把前n个碟子从第一个塔移动到第三个塔有两种方法: 1.把前n-1个移动到第二个塔,把第n个移动到第三个塔,然后把前n-1个从第二个移动到第三个: 2.把前n-1个移动到第三个塔,把第n个移动到第二个塔 ...

  8. CF392B Tower of Hanoi

    题目链接. Description 三塔汉诺塔问题,给一个 \(3 \times 3\) 的矩阵 \(t\),\(t_{i, j}\) 表示从 \(i\) 塔移动一个盘子到 \(j\) 塔的花费. 初 ...

  9. 227. Mock Hanoi Tower by Stacks【easy】

    In the classic problem of Towers of Hanoi, you have 3 towers and N disks of different sizes which ca ...

随机推荐

  1. NumPy数值计算(1)

    NumPy数值计算(1) 将列表转为NumPy中的array from __future__ import print_function from numpy import * import oper ...

  2. CodeForces839D[莫比乌斯反演] Codeforces Round #428 (Div. 2)

    /*CodeForces839D[莫比乌斯反演]*/ #include <bits/stdc++.h> typedef long long LL; const LL MOD = 10000 ...

  3. 从输入url到页面呈现的过程

    从输入url到页面呈现的过程包括两个基本过程:网络通信和页面渲染 网络通信主要过程是 域名解析 -> TCP连接 -> HTTP请求 -> 服务端响应,返回HTML 页面渲染的主要过 ...

  4. Python Base Two

    //fourth day to study python 24. In python , how to create funcation. we can use def to define funca ...

  5. ApplicationContext,WebApplicationContext

    servletContext 是web应用程序的大环境,用于存储整个web应用程序级别的对象. ApplicationContext,WebApplicationContext 是Spring的Bea ...

  6. 【前端学习笔记】2015-09-06 ~~~~ setAttribute()、slice()

    所遇记录: 1.setAttribute("属性",value),相同的还有addAttribute("属性名",value),getAttribute(“属性 ...

  7. 一款基于bootstrap的文件上传插件,现在很多手机webapp都在用

    官网:http://plugins.krajee.com/file-input

  8. 转 C++ 面向对象程序设计的基本特点

    传送门 Miss it   C++ 面向对象程序设计的基本特点 First: 抽象 面向对象方法中的抽象,是指对具体问题(对象)进行概括,抽出一类对象公共性质并加以描述的过程. 抽象的过程,也是对问题 ...

  9. XSD(XML Schema Definition)学习笔记

    今天学习了XSD相关的知识,为了以后查找的方便,写一些笔记. 一.什么是XSD? 1.XSD全称:XML Schema Definition.XML Schema 的作用是定义 XML 文档的合法构建 ...

  10. Android中沉浸式状态栏的应用

    在Android5.0版本后,谷歌公司为Android系统加入了很多新特性,刷新了Android用户的体验度.而其中的一个新特性就是沉浸式状态栏.那么问题来了,很多非移动端的小伙伴就要问了,什么是沉浸 ...