Hanoi T note
hanoi(n,x,y,z)
{
hanoi(n-1,x,z,y);//n-1 from x to y
move(x,z);//x->z
hanoi(n-1,y,x,z);//n-1 from y to z
}
hanoi(n-1,x,z,y)
{
hanoi(n-2,x,y,z);//n-2 from x to z
move(x,y);//n-1 from x to y
hanoi(n-2,z,x,y);//n-2 from z to y
}
the step move(x,y) is what you must do to realize hanoi(n-2,x,y,z),at last ,the last step will meet the first step that you can implement easily,this is the deepest I can comprehend
//11072013 add
对hanoi T的印象从大二接触C语言开始
其实算法非常简单,当盘子的个数为n时,移动的次数应等于2^n – 1(有兴趣的可以自己证明试试看)。后来一位美国学者发现一种出人//摘自百度
#include <stdio.h>
#define N 2 //N disks on original pillar 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)
{
hanoi('A', 'B', 'C', N);
return 0;
}
借鉴了其他人的思路,总算稍微理解了上述算法的实现,整理步骤要点
the fact is that wo don't konw how to do but konw wo must do,and push to opration to the stack memory until we move the topest disk to Z
a)step befor moving n to Z,the case is 1 to n-1 are on Y,the mothed is move n to Z,push the opration//the last opration
b)the last second situation is 1 to n-2 are on X,the mothed is move n-1 to Z,push the opration//second last opration
d)it is ease to see the first step is move n-(n-1) to X or Y
a and b is what we must do,and we have no other choices,but what has hanppend bettwen b and c,the stack has store all the oprations wo must do until c
assume the function can move 1 to n-1 to X or Y, so it can move 1 to n-2 to X or Y,and so on n-(n-1) to X or Y,it is easy to move n-(n-1) to X or Y
the reverse order you or the functon you build may implement,
e)n-1 disks on Y,put n on Z//after that,you can omit n on Z
f)n-2 disks on X,n-1 on Y//put the n-1 on Z,the question return to a,so a and b is the whole task need to resolve
****the last situation is 2 disks on X or Y
the whole steps is like ,if 1 OK,then 2OK;if 2 OK,then 3 OK
个人感悟:1,有时候踢皮球也是一种办法,此处是往上踢,有点默认路由的味道,没有别的选择
2,当你身处多级环境中,而且视野范围有限的情况下,只能虚构方法,即使不知道方法是怎么实现
3,the original case only permit taking one disk once,but it does't conflact with the step e and f,e and f is the situation we need implement but the mothed we use.The mothed is very simple,anyone can see directly,we image the situation first ,here the mothed is not the point
4,let the stack store the opration but not your head
5,用递归证明可以实现,用反证证明只有一种方式
Hanoi T note的更多相关文章
- Codeforces777E. Hanoi Factory 2017-05-04 18:10 42人阅读 评论(0) 收藏
E. Hanoi Factory time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Python学习札记(十四) Function4 递归函数 & Hanoi Tower
reference:递归函数 Note 1.在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. eg.计算阶乘: #!/usr/bin/env python3 def ...
- codeforces-777E Hanoi Factory (栈+贪心)
题目传送门 题目大意: 现在一共有N个零件,如果存在:bi>=bj&&bj>ai的两个零件i,j,那么此时我们就可以将零件j放在零件i上.我们现在要组成一个大零件,使得高度 ...
- poj 1920 Towers of Hanoi
Towers of Hanoi Time Limit: 3000MS Memory Limit: 16000K Total Submissions: 2213 Accepted: 986 Ca ...
- zoj 2954 Hanoi Tower
Hanoi Tower Time Limit: 2 Seconds Memory Limit: 65536 KB You all must know the puzzle named "Th ...
- Codeforces 777E:Hanoi Factory(贪心)
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a specia ...
- 三星Note 7停产,原来是吃了流程的亏
三星Note 7发售两个月即成为全球噩梦,从首炸到传言停产仅仅47天.所谓"屋漏偏逢连天雨",相比华为.小米等品牌对其全球市场的挤压.侵蚀,Galaxy Note 7爆炸事件这场连 ...
- 《Note --- Unreal --- MemPro (CONTINUE... ...)》
Mem pro 是一个主要集成内存泄露检测的工具,其具有自身的源码和GUI,在GUI中利用"Launch" button进行加载自己待检测的application,目前支持的平台为 ...
- 《Note --- Unreal 4 --- Sample analyze --- StrategyGame(continue...)》
---------------------------------------------------------------------------------------------------- ...
随机推荐
- mac 下 word 2011 使用笔记
1. 全屏预览,最大限度减少干扰 点击左下角焦点视图图标进入焦点视图. 调整为页面宽度或单页,点击顶部右侧百分比调整. 2.无格式粘贴 option + shift + command + v 3.无 ...
- 【翻译】CEDEC2014跨世代多平台并行开发PS4版如龙维新开发的一年
本篇PPT讲述的是如龙4的开发过程中,集中在PS3和PS4并行开发中所遇到和解决的一些问题.如64位指针,DX9向DX11移植API的问题,以及在PS4上使用并行渲染在1080P下让FPS达到60等. ...
- 浏览器 user-agent 字符串的故事
你是否好奇标识浏览器身份的User-Agent,为什么每个浏览器都有Mozilla字样? 故事还得从头说起,最初的主角叫NCSA Mosaic,简称Mosaic(马赛克),是1992年末位于伊利诺伊大 ...
- checkbox 全選、取消全選、反選
在寫一個全選.取消全選.反選的功能時. 未來元素[type='checkbox'],在使用了jquery-1.11.1.min.js版本的jquery時: 功能版本1: 存在的問題,當使用了attr時 ...
- 统计学习方法笔记 -- Boosting方法
AdaBoost算法 基本思想是,对于一个复杂的问题,单独用一个分类算法判断比较困难,那么我们就用一组分类器来进行综合判断,得到结果,"三个臭皮匠顶一个诸葛亮" 专业的说法, 强可 ...
- 二进制流 最后一段数据是最后一次读取的byte数组没填满造成的
while(in.read(temp)!=-1){ out.write(temp); } 改成: int len; while((len=in.read(temp))!=-1){out.write(t ...
- Android 网络连接判断与处理
Android网络连接判断与处理 获取网络信息需要在AndroidManifest.xml文件中加入相应的权限. <uses-permission android:name="and ...
- error: jump to label ‘XXXX’ [-fpermissive]
http://www.cnblogs.com/foohack/p/4090124.html 下面的类似的源码在MSVC上能正确编译通过.但是gcc/g++上就会错: 1. if(expr)2. got ...
- Union Find and search
1.POJ2488 A Knight's Journey search #include<iostream> #include<cstring> #include<alg ...
- Strom简介,以及安装,和官方案例测试
一:简介 1.strom的两种形式 2.strom的特性 3.使用场景 4.集群架构 5.集群架构进程 6.组件 Nimbus 7.从节点Supervisor 8.组件worker 9.组件Execu ...