递归求汉诺塔的解// 递归求汉诺塔的解.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"
#include
#include
#includevoid HanoiTower(int,char,char,char);
void main()
{
int n;
char A='A',B='B',...
分类:
其他好文 时间:
2015-07-31 18:32:43
阅读次数:
129
??
题目大意:就是普通的汉诺塔问题,给出n,表示说有n个大小不同的碟子,然后再给出每个碟子的初始位置和目标位置,要求计算出最少的步数使得每个碟子都移动到它的目标位置。
思路:考虑编号最大的盘子,如果它在初始位置和目标局面在同一根柱子上,那么我们不需要移动它。
由于盘子的移动是可逆的,根据对称性,我们只需要求出初始局面和目标局面移动形成的参考局面的步数之和,然后加一即可。
我们可以写一个函...
分类:
其他好文 时间:
2015-07-28 18:40:59
阅读次数:
124
/*
*功能:假设有3个塔座x y z,在x上插有n个直径大小各不相同、从小到大编号为1 - n的圆盘,要求将x轴上的n个圆盘移动到z轴并按同样顺序排列,移动圆盘须遵循以下规则:
1)、每次只能移动一个圆盘;
2)、圆盘可插在x y z中的任一塔座上;
3)、任何时刻不能将一个较大的圆盘压在较小的圆盘上;
*文件:hanoi.cpp
*时间:2015年7月6日20:22:29
*作者:cut...
分类:
其他好文 时间:
2015-07-26 22:45:36
阅读次数:
164
Perhaps you all have heard the mythical story about Tower of Hanoi (The details of this story is not required to solve this problem): “There is a towe...
分类:
其他好文 时间:
2015-07-26 20:29:47
阅读次数:
105
public void hanoi(int n) { if (n > 0) { func(n, "left", "mid", "right"); }}public void func(int n, String from, String mid, String to) { ...
分类:
其他好文 时间:
2015-07-26 18:41:38
阅读次数:
107
3.4 In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of different sizes which can slide onto any tower. The puzzle starts ...
分类:
其他好文 时间:
2015-07-26 12:25:22
阅读次数:
122
#includeint c = 0;//全局变量,搬动次数void move(char x, int n, char z){//第n个圆盘从塔座x搬到塔座z
printf("第%d步:将%i号从%c移到%c\n", ++c, n, x, z);
}void hanoi(int n, char x, char y, char z){//将塔座x上按直径由小到大且自上而下编号为...
分类:
其他好文 时间:
2015-07-25 15:23:06
阅读次数:
136
题意:汉诺塔问题,把A柱上n个圆盘全部移到B或C柱需要的步数,不同的是给出了一个序列,AB表示把A柱顶部的圆盘移到B柱顶上(按规则),这个序列每次从左到右扫描,找到可执行的第一个指令就执行一次,然后再从头扫描,同一个圆盘不能连续移动两次。问步数。
题解:因为序列给出,移动的顺序就是固定的,可以推出来。在序列不变的情况下,随着圆盘数量的增加,移动次数线性增加,可以得到如下递推式
f(1) = 1...
分类:
其他好文 时间:
2015-07-25 15:16:50
阅读次数:
247
建金字塔问题题目大意:Vanya got n cubes. He decided to build a pyramid from them. Vanya wants to build the pyramid as follows: the top level of the pyramid must ...
分类:
其他好文 时间:
2015-07-17 13:46:45
阅读次数:
141
1019: [SHOI2008]汉诺塔Time Limit:1 SecMemory Limit:162 MBSubmit:1039Solved:646[Submit][Status][Discuss]Description汉诺塔由三根柱子(分别用A B C表示)和n个大小互不相同的空心盘子组成。一开...
分类:
其他好文 时间:
2015-07-16 13:21:02
阅读次数:
112