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.
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3hiMDg0MTkwMTExNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
【Implement】
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的更多相关文章
- poj 3601 Tower of Hanoi
Tower of Hanoi Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 1853 Accepted: 635 De ...
- python递归三战:Sierpinski Triangle、Tower of Hanoi、Maze Exploring
本文已做成视频教程投稿b站(视频版相对文本版有一些改进),点击观看视频教程 本文主要通过三个实例来帮助大家理解递归(其展示动画已上传B站): 谢尔宾斯基三角形(Sierpinski Triangle) ...
- 汉诺塔问题(The Tower of Hanoi)的递归算法与非递归算法
非递归算法: 根据圆盘的数量确定柱子的排放顺序: 若n为偶数,按顺时针方向依次摆放 A B C: 若n为奇数,按顺时针方向依次摆放 A C B. 然后进行如下操作: (1)按顺时针方向把圆盘1从现在的 ...
- Tower of Hanoi问题
[问题描述] 有A, B, C三个塔座,A上套有n个直径不同的圆 盘,按直径从小到大叠放,形如宝塔,编号1, 2, 3 … n. 要求将n个圆盘从A移到C,叠放顺序不变,移动过程中遵循 下列原则: w ...
- [POJ1958][Strange Tower of Hanoi]
题目描述 求解 \(n\) 个盘子 \(4\) 座塔的 Hanoi 问题最少需要多少步 问题分析 考虑 \(3\) 座塔的 Hanoi 问题,记 \(f[i]\) 表示最少需要多少步, 则 \(f[i ...
- 汉诺塔 Tower of Hanoi
假设柱子标为A,B.C.要由A搬至C,在仅仅有一个盘子时,就将它直接搬至C:当有两个盘子,就将B作为辅助柱.假设盘数超过2个.将第二个下面的盘子遮起来,就非常easy了.每次处理两个盘子,也就是:A- ...
- codeforces 392B Tower of Hanoi
把前n个碟子从第一个塔移动到第三个塔有两种方法: 1.把前n-1个移动到第二个塔,把第n个移动到第三个塔,然后把前n-1个从第二个移动到第三个: 2.把前n-1个移动到第三个塔,把第n个移动到第二个塔 ...
- CF392B Tower of Hanoi
题目链接. Description 三塔汉诺塔问题,给一个 \(3 \times 3\) 的矩阵 \(t\),\(t_{i, j}\) 表示从 \(i\) 塔移动一个盘子到 \(j\) 塔的花费. 初 ...
- 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 ...
随机推荐
- 聊聊、Nginx 初始化错误信息
这篇文章我们继续学习 main 方法,我们先来看看 ngx_debug_init() 这个方法. 从方法名我们也知道,debug初始化.我们先看看方法位置在哪.我们来断点在这个方法上面. Functi ...
- Clarke and five-pointed star
Clarke is a patient with multiple personality disorder. One day, Clarke turned into a learner of geo ...
- 【mysql 优化 1】优化概述
原文地址:Optimization Overview 数据库性能取决于几个数据库层面的因素,比如:表设计,查询语句,配置. 这些软件结构导致你必须在CPU和I/O 操作的硬件层面做到尽可能的最小化和高 ...
- CS231n笔记 Lecture 1 Introduction
主题有关 这一讲主要是介绍性质的,虽然大多数概念以前听说过,但还是在他们的介绍中让我有如下一些认识,所谓温故而知新,不无道理: IMAGENET Feifei Li的团队首先爬取.标注了IMAGENE ...
- C#递归删除进程及其子进程
/// <summary> /// 结束进程和相关的子进程 /// </summary> /// <param name="pid">需要结束的 ...
- 【python自动化】python 常用时间获取方法
代码如下: import datetime import time DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" DATE_FORMAT = &quo ...
- 马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动(转)
马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作 马士兵hadoop第三课:java开发hdfs 马士兵hadoop第 ...
- 两个时间相差多少 .net中的timespan应用
原文发布时间为:2008-10-31 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- vue.js源码学习分享(六)
/* */ /* globals MutationObserver *///全局变化观察者 // can we use __proto__?//我们能用__proto__吗? var hasProto ...
- php--转码函数
最近在用dedecms二次开发会员功能:大家都知道dedecms编码是GBK格式的:所以在我们在项目中经常需要转码,在我了解中有两种转码方式:一是:iconv:二是mb_convert_encodin ...