当前位置:网站首页>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
边栏推荐
- VictoriaMetrics 简介
- 2019 Nanchang (relive the classic)
- 分享一下如何制作专业的手绘电子地图
- 100 important knowledge points that SQL must master: management transaction processing
- Find objects you can't see! Nankai & Wuhan University & eth proposed sinet for camouflage target detection, and the code has been open source
- Web侧防御指南
- Redis分布式锁故障,我忍不住想爆粗...
- 图像基础概念与YUV/RGB深入理解
- Pyqt picture decodes and encodes and loads pictures
- How to write a good program when a big book speaks every day?
猜你喜欢

Daily book -- analyze the pain points of software automation from simple to deep

*C语言期末课程设计*——通讯录管理系统(完整项目+源代码+详细注释)

MySQL learning record (8)

From "bronze" to "King", there are three secrets of enterprise digitalization

Interpretation of CVPR paper | generation of high fidelity fashion models with weak supervision

Lightgbm principle and its application in astronomical data

The difference between include < > and include ""

《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!

scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文

"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
随机推荐
The neo4j skill tree was officially released to help you easily master the neo4j map database
MySQL learning record (3)
"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!
20220702 how do programmers build knowledge systems?
How to prevent your jar from being decompiled?
Daily book CSO advanced road first exposed
100 important knowledge points that SQL must master: management transaction processing
[Jianzhi offer] 56 - ii Number of occurrences of numbers in the array II
Ransack组合条件搜索实现
Five message formats of OSPF
MySQL learning record (9)
Physical layer cables and equipment
Daily book -- analyze the pain points of software automation from simple to deep
将 EMQX Cloud 数据通过公网桥接到 AWS IoT
SQL必需掌握的100个重要知识点:使用游标
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
LandingSite eBand B1冒烟测试用例
A specially designed loss is used to deal with data sets with unbalanced categories
SQL必需掌握的100个重要知识点:管理事务处理
[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)