Hanoi Tower问题分析

来源:互联网 发布:sql server 2005 iis 编辑:程序博客网 时间:2024/06/05 19:52

前言

回家休息第3天了,状态一直不是太好,主要是要补牙,检查身体,见同学见亲戚,心里又着急校招,难得能腾出时间来好好思考,这里也是看<cracking the coding interview>,看到了汉诺塔问题,这里记录一下

思路分析

汉诺塔是递归的经典题目,这里先介绍使用递归的关键:

使用递归的一个关键就是:我们先定义一个函数,不要着急去实现它,但是要明确它的功能

对于汉诺塔问题,我们定义如下函数原型:

void hanoi(char src, char mid, char dst, int n)

我们先不要在意它是如何实现的这种细节,而是先明确一下它的功能:

将n个盘子从柱子src移动到柱子dst,其中可以借助柱子mid(middle 中间件)

既然要用到递归,当然是在这个函数中用到了函数本身.也就是说,我们在完成这个任务的步骤中还会用到haoni这个操作,只是参数可能不一样而已.

我们定义一个元组来表示三根柱子的状态:(src, mid, dst),数量为每根柱子上的圆盘数量:
  1. 初始状态: (n, 0, 0)
  2. 目标状态: (0, 0, n - 1)
  3. 中间状态: (n, n - 1, 0)
只有存在中间状态,才能将最大盘子n从src移动到dst,然后再将mid上的n-1个盘子移回到src上,这样问题规模就从n减少到n - 1了

  1. 从初始状态到中间状态使用操作 hanoi(src, dst, mid, n - 1)即可.即把n - 1个盘子从src移动到mid,中间借助dst
  2. 接下来,就是将圆盘n从src移动到dst即可 printf("Move disk %d from %c to %c", n, src, dst);
  3. 上面操作完成后,得到的状态是(0, n - 1, n)
  4. 接下来,要将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;}

汉诺塔变形

题目描述:约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下、由小到大顺序串着由64个圆盘构成的塔。目的是将最左边杆上的盘全部移到右边的杆上,条件是一次只能移动一个盘,且不允许大盘放在小盘的上面。现在我们改变游戏的玩法,不允许直接从最左(右)边移到最右(左)边(每次移动一定是移到中间杆或从中间移出),也不允许大盘放到下盘的上面。Daisy已经做过原来的汉诺塔问题和汉诺塔II,但碰到这个问题时,她想了很久都不能解决,现在请你帮助她。现在有N个圆盘,她至少多少次移动才能把这些圆盘从最左边移到最右边?输入:包含多组数据,每次输入一个N值(1<=N=35)。输出:对于每组数据,输出移动最小的次数。样例输入:1312样例输出:226531440

思路

我们要紧记递归的精髓: 先定义一个函数,不要在意实现细节,先明确函数的功能

首先,我们分析一下这种汉诺塔变形有几种状态变化(src, bri, dst)来表示:

  1. (1 ~ n, 0, 0)  --> 初始状态
  2. (n, 0, 1 ~ n - 1)  --> 将n-1个盘子从src移动到dst(中间是先send到bri,再send到dst)
  3. (0, n, 1 ~ n - 1)  --> 将第n个盘子从src移动到bri
  4. (1 ~ n - 1, n, 0) --> 将dst上的n - 1个盘子移回src
  5. (1 ~ n - 1, 0, n) --> 将bri上的第n个盘子移动到dst
  6. (0, 0, 1 ~ n) --> 将src上的剩余n - 1个盘子移动到dst(问题规模减少)

我们定义一个函数:

void move(char src, char dst, int n)

表示把n个盘子从src移动到dst

src和dst之间的盘子移动借助函数:

void send(char src, char dst, int num){    // src先移动到bri, bri再移动到dst}

所以上述的状态可以用move函数和send函数表示出来,具体的递归细节就不要纠结了

代码

#include <stdio.h>#include <stdlib.h>long int count;void send(char src, char dst, int num){printf("Move %d disk from %c to %c!\n", num, src, dst);count += 1;}void move(char src, char dst, int n){if (n == 1) {send(src, 'B', n);send('B', dst, n);} else {move(src, dst, n - 1);send(src, 'B', n);move(dst, src, n - 1);send('B', dst, n);move(src, dst, n - 1);}}int main(void){int n;while (scanf("%d", &n) != EOF) {count = 0;move('A', 'C', n);printf("%ld\n", count);}return 0;}


参考链接

http://www.cnblogs.com/yanlingyin/archive/2011/11/14/2247594.html
原创粉丝点击