Hanoi Tower问题分析
前言
思路分析
void hanoi(char src, char mid, char dst, int n)
我们先不要在意它是如何实现的这种细节,而是先明确一下它的功能:
将n个盘子从柱子src移动到柱子dst,其中可以借助柱子mid(middle 中间件)
既然要用到递归,当然是在这个函数中用到了函数本身.也就是说,我们在完成这个任务的步骤中还会用到haoni这个操作,只是参数可能不一样而已.
- 初始状态: (n, 0, 0)
- 目标状态: (0, 0, n - 1)
- 中间状态: (n, n - 1, 0)
- 从初始状态到中间状态使用操作 hanoi(src, dst, mid, n - 1)即可.即把n - 1个盘子从src移动到mid,中间借助dst
- 接下来,就是将圆盘n从src移动到dst即可 printf("Move disk %d from %c to %c", n, src, dst);
- 上面操作完成后,得到的状态是(0, n - 1, n)
- 接下来,要将mid上的n - 1个盘子移动到dst上,用hanoi(mid, src, dst, n - 1)
递归代码
/**
* In the classic problem of the Towers of Hanoi, you have 3 rods and N disks of different
* sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending
* order of size from top to bottom. You have the following constraints:
* (A) Only one disk can be moved at a time
* (B) A disk is slid off the top of one rod onto the next rod
* (C) A disk can only be placed on top of a larger disk
* Write a program to move the disks from the first rod to the last using stacks
*/ #include <stdio.h>
#include <stdlib.h> /**
* 递归代码
*
* T = O(2^n - 1) 可推导证明
*
*
*/
void hanoi(char src, char mid, char dst, int n)
{
if (n == 1) {
printf("Move disk %d from %c to %c\n", n, src, dst);
} else {
hanoi(src, dst, mid, n - 1);
printf("Move disk %d from %c to %c\n", n, src, dst);
hanoi(mid, src, dst, n - 1);
}
} int main(void)
{
int n; while (scanf("%d", &n) != EOF) {
hanoi('A', 'B', 'C', n);
} return 0;
}
参考链接
Hanoi Tower问题分析的更多相关文章
- HDU1329 Hanoi Tower Troubles Again!——S.B.S.
Hanoi Tower Troubles Again! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- ZOJ-1239 Hanoi Tower Troubles Again!
链接:ZOJ1239 Hanoi Tower Troubles Again! Description People stopped moving discs from peg to peg after ...
- Codeforces Gym 100114 A. Hanoi tower 找规律
A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...
- 汉诺塔 Hanoi Tower
电影<猩球崛起>刚开始的时候,年轻的Caesar在玩一种很有意思的游戏,就是汉诺塔...... 汉诺塔源自一个古老的印度传说:在世界的中心贝拿勒斯的圣庙里,一块黄铜板上插着三支宝石针.印度 ...
- HDU 1329 Hanoi Tower Troubles Again!(乱搞)
Hanoi Tower Troubles Again! Problem Description People stopped moving discs from peg to peg after th ...
- 3-6-汉诺塔(Hanoi Tower)问题-栈和队列-第3章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第3章 栈和队列 - 汉诺塔(Hanoi Tower)问题 ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版> ...
- 1028. Hanoi Tower Sequence
1028. Hanoi Tower Sequence Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Hanoi Tow ...
- Python学习札记(十四) Function4 递归函数 & Hanoi Tower
reference:递归函数 Note 1.在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. eg.计算阶乘: #!/usr/bin/env python3 def ...
- zoj 2954 Hanoi Tower
Hanoi Tower Time Limit: 2 Seconds Memory Limit: 65536 KB You all must know the puzzle named "Th ...
随机推荐
- java运用FFMPEG视频转码技术
基于windows系统安装FFMPEG转码技术 http://wenku.baidu.com/link?url=z4Tv3CUXxxzLpa5QPI-FmfFtrIQeiCYNq6Uhe6QCHkU- ...
- C# 多线程传参 三种实例
//using Thread to download files //1111111111111111 foreach (var str in listDownloadPdf) { //string ...
- iBeacon开发
什么是iBeacons iBeacons是苹果在2013年WWDC上推出一项基于蓝牙4.0(Bluetooth LE | BLE | Bluetooth Smart)的精准微定位技术,当你的手持设备靠 ...
- 找出图像I的代数中心
function centerGPos = cenP(I ) %cenP finds the core of the PSF % [row, col] = find(I > ); minRow ...
- 内核源码分析之软中断(基于3.16-rc4)
1.和软中断相关的数据结构: softing_vec数组(kernel/softirq.c) static struct softirq_action softirq_vec[NR_SOFTIRQS] ...
- django form关于clean及cleaned_data的说明 以及4种初始化
1.form类的运行顺序是init,clean,validte,save其中clean和validate会在form.is_valid()方法中被先后调用.(这里留有一个疑问,结构完全相同的两个f ...
- The First Pig Task
The First Pig Program 环境: Hadoop-1.1.2 pig-0.11.1 linux系统为CentOS6.4 jdk1.6 在伪分布 ...
- 那些经常被遗忘的 Java 面试题
静态类和静态方法 如果一个类要被声明为static的,只有一种情况,就是静态内部类. 静态内部类实际上与普通类(即类名必须与文件名一样的顶级类)一样,只是静态内部类在某一类的内部定义了而已,既然是类, ...
- MANACHER---求最长回文串
求最长回文串,如果是暴力的方法的话,会枚举每个字符为中心,然后向两边检测求出最长的回文串,时间复杂度在最坏的情况下就是0(n^2),为什么时间复杂度会这么高,因为对于每一个作为中心的字符的检测是独立的 ...
- How to include cascading style sheets (CSS) in JSF
In JSF 2.0, you can use <h:outputStylesheet /> output a css file. For example, <h:outputSty ...