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 ...
随机推荐
- Python字典类型、
字典类型: # msg_dic = {# 'apple': 10,# 'tesla': 100000,# 'mac': 3000,# 'lenovo': 30000,# ...
- HDU——1272小希的迷宫(并查集+拓扑排序)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- Redis Cluster 集群的实现和管理
系统环境 CentOS 7 集群规划 在一台物理机(实际部署应当分散到多个物理机上),创建6个Redis节点,其中3个主节点.3个从节点. 节点表: IP 端口 主从 路径 192.168.1.21 ...
- Spring定义的五种事务隔离级别
在Spring中定义了5中不同的事务隔离级别. 1. ISOLATION_DEFAULT(一般情况下使用这种配置既可) 这是一个PlatfromTransactionManager默认的隔离级别,使用 ...
- SpringBoot之ApplicationContextInitializer的理解和使用
一. ApplicationContextInitializer 介绍 首先看spring官网的介绍: 翻译一下: 用于在spring容器刷新之前初始化Spring ConfigurableAppli ...
- jquery 日期插件
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- LeetCode OJ——Binary Tree Inorder Traversal
http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ 树的中序遍历,递归方法,和非递归方法. /** * Definition ...
- npm-debug.log文件出现原因
项目主目录下总是会出现这个文件,而且不止一个,原因是npm i 的时候,如果报错,就会增加一个此文件来显示报错信息,npm install的时候则不会出现.
- Redis实用监控工具一览
Redis已经成为web应用开发不可或缺的一个组成部分,在项目中的应用越来越广泛,这篇文章就来讲讲那些关于Redis监控的那点事. vredis-benchmark 1.1 简介 第一个就介绍一下,R ...
- SpringMVC整合MongoDB
首先,在pom文件中新增spring-data-mongodb的依赖: <dependency> <groupId>org.springframework.data</g ...