当前位置:网站首页>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
边栏推荐
- Interpretation of CVPR paper | generation of high fidelity fashion models with weak supervision
- Bridge emqx cloud data to AWS IOT through the public network
- 20220702 how do programmers build knowledge systems?
- pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
- 如何访问kubernetes API?
- 地理探测器原理介绍
- Centos7 installation and configuration of redis database
- 情感计算与理解研究发展概述
- scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
- Les trois principaux points de douleur traités par servicemesh
猜你喜欢

Basic IO interface technology - microcomputer Chapter 7 Notes

《Just because》阅读感受

MySQL learning record (3)

Ransack组合条件搜索实现

The failure rate is as high as 80%. What should we do about digital transformation?

kubernetes资源对象介绍及常用命令(四)

C语言,实现三子棋小游戏

Redis distributed lock failure, I can't help but want to burst

GEE:(二)对影像进行重采样

*C language final course design * -- address book management system (complete project + source code + detailed notes)
随机推荐
Reading experience of just because
一周生活
Attack and defense world PWN question: Echo
[Jianzhi offer] 57 And are two numbers of S
[shutter] shutter layout component (wrap component | expanded component)
How to write a good program when a big book speaks every day?
Service visibility and observability
Pip install whl file Error: Error: … Ce n'est pas une roue supportée sur cette plateforme
Gee: (II) resampling the image
Off chip ADC commissioning record
【C 题集】of Ⅴ
Detailed explanation of OSI seven layer model
关于PHP-数据库的 数据读取,Trying to get property 'num_rows' of non-object?
Analysis of neural network
Gbase8s database type
Les trois principaux points de douleur traités par servicemesh
Redis distributed lock failure, I can't help but want to burst
About test cases
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
ArrayList分析2 :Itr、ListIterator以及SubList中的坑