当前位置:网站首页>Hanoi Tower problem -- > recursive implementation
Hanoi Tower problem -- > recursive implementation
2022-07-19 05:42:00 【numb and dying】
One 、 The source of the 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 ( Pictured 1). 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 .
Two 、 The solution to the problem
analysis : For such a question , No one can write every step of moving the plate directly , But we can use the following methods to solve . Set the number of moving plates to n, To put this n A plate from A The rod moves to C rod , You can do the following three steps :
(1) With C Disk as an intermediary , from A Rod will 1 to n-1 Disk No. moved to B rod ;
(2) take A The remaining third in the rod n Disk No. moved to C rod ;
(3) With A The rod is the intermediary ; from B Rod will 1 to n-1 Disk No. moved to C rod .
In practice , Only the second step can be done directly , And first 、 Three steps have become a new problem of mobile . The essence of the above operation is to move n The problem of a plate turns into moving n-1 A plate , That one 、 Three steps to solve ? in fact , The above method sets the number of plates as n, n Can be any number , The same applies to mobile phones n-1 A plate . therefore , According to the above law , To solve n -1 A plate from A Move lever to B rod ( First step ) Or from B Move lever to C rod ( The third step ) problem . Now? , The problem is solved by moving n The operation of a plate is transformed into moving n-2 The operation of a plate . According to this principle , Layer by layer recursion , You can turn the original problem into a mobile solution n -2、n -3… … 3、2, Until you move 1 Operation of a disk , The operation of moving a disk can be completed directly . thus , The tower of Hanoi problem was solved . And this is complicated and simplified , A method of solving complex problems with simple problems and known operations , It's recursion .
3、 ... and 、 The steps of Hanoi Tower problem ( Divided into two steps )
First step :
hold n-1 A plate From tower 1 Move to the tower 2
The first n A plate From tower 1 Move to the tower 3
The second step :
hold n-1 A plate From tower 2 Move to the tower 3
Four 、 Code implementation
#include <stdio.h>
void Hanoi(int n, char* left, char* mid, char* right,int* p) {
if (n == 1) {
printf("%s Move to %s\n", left, right);
(*p) += 1;
}
else {
han_nuo(n-1, left, right, mid, p);
printf("%s Move to %s\n", left, right);
(*p)++;
han_nuo(n-1, mid, left, right, p);
}
}
int main() {
int n = 0;
int count = 0;
char arr[][5] = { " TA Yi "," Tower II "," Tower three " };
printf(" Please enter the number of floors of Hanoi tower :");
scanf("%d", &n);
Hanoi(n,arr[0], arr[1], arr[2], &count);
printf(" It needs to be moved altogether %d Time \n", count);
return 0;
}边栏推荐
- 微信小程序之计算器
- MySQL -- storage and cursor
- Problems encountered by kotlin generics
- class文件格式的理解
- Common (Consortium)
- ES6 adds -let and const (disadvantages of VaR & let and const)
- Pointnet++代码详解(二):square_distance函数
- 【语音识别入门】基础概念与框架
- 对Crowdhuman数据集处理,根据生成的train.txt分离数据集
- Edge AI边缘智能:Communication-Efficient Edge AI: Algorithms and Systems(未完待续)
猜你喜欢

5. Business analysis of data acquisition channel construction

软件过程与管理总复习

6.数据仓库搭建之数据仓库设计

微信小程序中的WXML模板语法

Use ide to make jar package

Page navigation of wechat applet

软件过程与管理复习(七)

Pointnet++代码详解(七):PointNetSetAbstractionMsg层

Unable to determine Electron version. Please specify an Electron version

Calculator of wechat applet
随机推荐
INRIAPerson数据集转化为yolo训练格式并可视化
Review my first job search trip
2021-05-21
MySQL learning notes (4) - (basic crud) operate the data of tables in the database
11. DWS layer construction of data warehouse construction
软件过程与管理复习(七)
记录:YOLOv5模型剪枝轻量化
运行基于MindSpore的yolov5流程记录
12. Ads layer construction of data warehouse construction
7. Data warehouse environment preparation for data warehouse construction
throttle/debounce应用及原理
Object to map
Advanced operation of C language
SQL练习题集合
cuda11.0的pytorch安装小计
编程风格
【语音识别入门】基础概念与框架
Using Flink SQL to fluidize market data 2: intraday var
5. Spark core programming (1)
Spark core programming (4) -- spark operation architecture
