当前位置:网站首页>Hanoi Tower problem
Hanoi Tower problem
2022-07-02 22:16:00 【HairLossException】
The origin of the tower of Hanoi problem
It's said that in the ancient temple of India , There is one called the Hanoi Tower (Hanoi) The game of . The game is on a copper device , There are three rods ( Number A、B、C), stay A From the bottom up 、 Place in order from large to small 64 A gold plate . The goal of the game : hold A All the gold plates on the pole are moved to C On the pole , And still keep the original order . Operating rules : Only one plate can be moved at a time , And keep the big plate down all the time during the moving process , Small in , The plate can be placed during the operation A、B、C On any pole .
Thought analysis
If there is only one disc, move the disc directly from the initial column to the target column
The number of discs is 2 Move the small disc to the auxiliary column, and then move the large disc to the target column
If you have any n Disc We can put this n A disc is regarded as 2 A plate —— The bottom plate and all the upper plates
(1) The above n-1 A disk moves to B column ( here A The column is the initial column ,B The column is the target column ,C Columns are auxiliary columns )
(2) Move the lowest disc to the target column ( here A The column is the initial column ,C The column is the target column ,B Columns are auxiliary columns )
(3) Move all disks on the auxiliary column to the target disk ( here B The column is the initial column ,C The column is the target column ,A Columns are auxiliary columns )
public class HanoiTower {
public static void main(String[] args) {
hanoiTower(4,'A','B','C');
}
/** * * @param num Number of discs * @param a Starting column * @param b Auxiliary column * @param c Target column */
public static void hanoiTower(int num,char a,char b,char c){
/* If there is only one disc, the disc can be directly removed from a The column moves to c column */
if (num == 1){
System.out.println(" The first 1 A disk from " + a + "->" + c);
}else{
/* The number of discs is greater than 1*/
/* First move all the disks above to from A Move to B column C Columns as auxiliary columns */
hanoiTower(num-1,a,c,b);
/* Move the bottom disk to C column */
System.out.println(" The first "+num+" A disk from " + a + "->" + c);
/* hold B The disc on the column moves to C column A Columns are auxiliary columns */
hanoiTower(num-1,b,a,c);
}
}
}
Running results
The first 1 A disk from A->B
The first 2 A disk from A->C
The first 1 A disk from B->C
The first 3 A disk from A->B
The first 1 A disk from C->A
The first 2 A disk from C->B
The first 1 A disk from A->B
The first 4 A disk from A->C
The first 1 A disk from B->C
The first 2 A disk from B->A
The first 1 A disk from C->A
The first 3 A disk from B->C
The first 1 A disk from A->B
The first 2 A disk from A->C
The first 1 A disk from B->C
Process finished with exit code 0
边栏推荐
- Daily book CSO advanced road first exposed
- Sql service intercepts string
- ArrayList分析2 :Itr、ListIterator以及SubList中的坑
- 使用 EMQX Cloud 实现物联网设备一机一密验证
- 在beforeDestroy中销毁localStorage中的值无效
- Pyqt picture decodes and encodes and loads pictures
- Blue Bridge Cup Winter vacation homework (DFS backtracking + pruning)
- Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring
- "Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!
- [zero foundation I] Navicat download link
猜你喜欢
Blue Bridge Cup Winter vacation homework (DFS backtracking + pruning)
如何防止你的 jar 被反编译?
"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!
The difference between include < > and include ""
[shutter] shutter layout component (fractionallysizedbox component | stack layout component | positioned component)
GEE:(二)对影像进行重采样
[Yu Yue education] reference materials of analog electronic technology of Nanjing Institute of information technology
Basic IO interface technology - microcomputer Chapter 7 Notes
The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
关于测试用例
随机推荐
Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file
C language, to achieve three chess games
pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform
使用 EMQX Cloud 实现物联网设备一机一密验证
Official announcement! The golden decade of new programmers and developers was officially released
C语言,实现三子棋小游戏
Evolution of messaging and streaming systems under the native tide of open source cloud
【剑指 Offer】56 - I. 数组中数字出现的次数
The difference between include < > and include ""
Daily book CSO advanced road first exposed
[zero foundation I] Navicat download link
About test cases
技术人创业:失败不是成功,但反思是
【C 题集】of Ⅴ
发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...
Gee: (II) resampling the image
:last-child 不生效解决
MySQL learning record (9)
Centos7 installation and configuration of redis database
Gbase8s database type