当前位置:网站首页>C language: recursive function to realize Hanoi Tower
C language: recursive function to realize Hanoi Tower
2022-06-13 09:12:00 【Caixinzhi】
If you are not familiar with recursion, you can learn about it in the previous articles .
Hanoi (Hanoi) What is it? ?

A simple Hanoi tower is shown in the figure above , There are three placement points , The placement must follow the rules of small up and large down , In turn 1 All the objects in the are placed in 3 in . For example, there is 4 A placer , If the A All the objects placed on the are moved to C On , The specific steps are :A->B A->C B->C A->B C->A C->B A->B A->C B->C B->A C->A B->C A->B A->C B->C.
that ,C How does language realize the tower of Hanoi ?
The first step is to determine the starting position 、 Transfer location 、 Destination location , In the first place A It's the starting position ,B It's a transit location ,C Is the destination location , The function of these three positions will be changed all the time when moving the object block , To achieve the ultimate goal .
The basic idea of Hanoi tower is :
The first stage : take n-1 A block ( That is, except for the bottom block ) After a series of stacking ( Here we can use the recursive method to realize ), Finally, put it on the transfer position , Then put the remaining blocks at the starting position on the destination position , Here's the picture :
——> 
The above series of stacking means : With A Is the starting position ,C It is a transit location ,B For the purpose of location , It's equivalent to putting C Think of it as an intermediate repository , To help here n-1 Put a block in B Go inside .
The second stage : And then you'll find out , After the change, the form of Hanoi tower is the same as before , If you put B As a starting position ,A It's a transit location ,C Is the destination location . You can always recurse according to the above method , Here's the picture :
——>
And so on …… Finally, all the objects can be removed from A Moved to C.
See below for the specific code ( Note the following in the code ):
//C Language implementation of Hanoi Tower
#include <stdio.h>
void move(char p1, char p2)
{
printf("%c->%c ", p1, p2);
}
//n: Number pos1: The starting position pos2: Transfer location pos3: Destination location
void Hanoi(int n, char pos1, char pos2, char pos3)
{
if (n == 1)
{
move(pos1, pos3);
}
else
{
Hanoi(n - 1, pos1, pos3, pos2);
move(pos1, pos3);
Hanoi(n - 1, pos2, pos1, pos3);
}
}
int main()
{
Hanoi(1, 'A', 'B', 'C');
printf("\n");
Hanoi(2, 'A', 'B', 'C');
printf("\n");
Hanoi(3, 'A', 'B', 'C');
printf("\n");
Hanoi(4, 'A', 'B', 'C');
printf("\n");
return 0;
}Pay attention to point one : In code n It's not worth much , Because the number of moves varies with n The increase of is exponential .
Pay attention to point two :n by 1 It's almost time to reach the minimum unit ( That is, the bottom ), There is no need to use recursion ;n For more than 1 The value of , Need to recurse to 1 To carry out .
Pay attention to three : The first stage uses recursion to express the above n-1 Move layers all to B in ; The second stage uses recursion to express B Move all to C in .
In this way, the tower of Hanoi can be simply completed , This code is not the best approach , But it's easy to understand . Recursion is not used very much in practice , But you should also understand the code and write the basic code of recursive functions such as Hanoi tower .
边栏推荐
- 教程篇(5.0) 04. Fortint云服务和脚本 * FortiEDR * Fortinet 网络安全专家 NSE 5
- Margin:0 reason why auto does not take effect
- Drill down to protobuf - Introduction
- 20211104 为什么矩阵的迹等于特征值之和,为什么矩阵的行列式等于特征值之积
- Map 23 summary
- Pytorch model tuning - only some layers of the pre training model are loaded
- 13.inline,const,mutable,this,static
- Simulink variant model and variant subsystem usage
- 简单实现数据库链接池
- LeetCode 583. 两个字符串的删除操作
猜你喜欢

【 sécurité 】 comment devenir ingénieur de sécurité de 0 à 1 contre - attaque pour la Fondation zéro

How excel adds hyperlinks to some text in a cell

Installation of sonarqube code quality management platform (to be continued)

Exporting MySQL data table documents using Navicat

【安全】零基礎如何從0到1逆襲成為安全工程師

CAS无锁

Jfinal and swagger integration

CAS NO lock

Completely uninstall PostgreSQL under Linux

JUC原子引用与ABA问题
随机推荐
20220606 关于矩阵的Young不等式
Redis fuzzy query batch deletion
教程篇(5.0) 04. Fortint云服务和脚本 * FortiEDR * Fortinet 网络安全专家 NSE 5
C language time difference calculation
Neo4j環境搭建
教程篇(5.0) 03. 安全策略 * FortiEDR * Fortinet 网络安全专家 NSE 5
线上调试工具Arthas基础
You don't know the usage of stringstream
Neo4j environment construction
How many TCP connections can a machine create at most?
20211006 integral, differential and projection belong to linear transformation
Cisco, Huawei network equipment
【安全】零基础如何从0到1逆袭成为安全工程师
How Simulink adds modules to the library browser
20211115 任意n阶方阵均与三角矩阵(上三角或者下三角)相似
BGP Federation +community
20211104 为什么矩阵的迹等于特征值之和,为什么矩阵的行列式等于特征值之积
20211020 academician all drive system
LeetCode 1143. 最长公共子序列
20211104 why are the traces of similar matrices the same