当前位置:网站首页>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
边栏推荐
- Etcd Raft 协议
- The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
- MySQL learning record (5)
- Introduction to victoriametrics
- Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
- Pip install whl file Error: Error: … Ce n'est pas une roue supportée sur cette plateforme
- Pyqt picture decodes and encodes and loads pictures
- Blue Bridge Cup Eliminate last one (bit operation, code completion)
- 【剑指 Offer】56 - I. 数组中数字出现的次数
- 情感计算与理解研究发展概述
猜你喜欢

MySQL learning record (2)

Basic concepts of image and deep understanding of yuv/rgb

LightGBM原理及天文数据中的应用

Daily book - low code you must understand in the era of digital transformation

Off chip ADC commissioning record

A specially designed loss is used to deal with data sets with unbalanced categories

PIP audit: a powerful security vulnerability scanning tool

CVPR论文解读 | 弱监督的高保真服饰模特生成

【零基础一】Navicat下载链接

Etcd raft protocol
随机推荐
加了定位的文字如何水平垂直居中
"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 book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
如何防止你的 jar 被反编译?
ServiceMesh主要解决的三大痛点
VIM command-t plugin error: unable to load the C extension - VIM command-t plugin error: could not load the C extension
Web侧防御指南
MySQL learning record (9)
MySQL learning record (3)
Introduction to the principle of geographical detector
PIP audit: a powerful security vulnerability scanning tool
The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
: last child does not take effect
暑期第一周总结
From personal heroes to versatile developers, the era of programmer 3.0 is coming
Share how to make professional hand drawn electronic maps
20220702 how do programmers build knowledge systems?
VictoriaMetrics 简介
pyqt图片解码 编码后加载图片
C language, to achieve three chess games