当前位置:网站首页>Hanoi Tower problem
Hanoi Tower problem
2022-06-27 19:50:00 【Bitongo】
List of articles
Preface
Modern cognitive psychology is an experiment used to study the psychological characteristics of people's problem-solving process . The initial state of Hanoi Tower problem is A、B、C The three pillars , stay A There is a hole in the middle of the column, which overlaps from large to small and from bottom to top “ tower ” The same number of discs . The target state is to “ tower ” Move to C On the column ,B Column as transition . The rule is that only the top disc can be moved at a time , The big disc cannot be pressed on the small disc . It is required to explore the path from the initial state to the target state , Finally solve the problem , State of achievement . The subjects thought , While reporting out loud what you think . According to the oral report of the subjects , Analyze their thinking process .
One 、Hanoi What is the tower problem ?
Hanoi Tower problem is a classical experiment in problem solving . Give column 1、2、3, In the column 1 On , There is a series of discs , The size of the top-down disc is increasing , Form a pyramid . The subjects are required to put the column 1 All the disks of move to the column 3 Up , And finally in the column 3 It still forms a pyramid arrangement , The rule is that only one disc can be moved at a time , And the market cannot be pressed on the small market , You can use a cylinder 2. The minimum number of movements to complete Hanoi tower operation is 2-1 Time , among n Is the number of discs .
Two 、 Problem description
There are three needles on one board A、B、C.A The needle is covered with a needle 64 A disc of different sizes , According to the big one 、 The small ones are arranged in the order of , I want to put this 64 A disk from A The needle moves to C On the needle , Only one disc can be moved at a time , The movement process can be done with the help of B The needle . But at any time , Any disc on the needle must keep the big one down , Small in . Enter the number of disks to be moved from the keyboard , Give the process of moving .
3、 ... and 、 Algorithmic thought
When only one disc is moved , Direct the disc from A The needle moves to C The needle .
If the moving disk is n(n>1), It is divided into several steps :
- hold (n-1) A disk from A The needle moves to B The needle ( With the help of C The needle );
- A The last disc on the needle moves to C The needle ;
- B On the needle (n-1) A disk moves to C The needle ( With the help of A The needle ).
Every time I do it , One less moving disc , Gradually decreasing , Finally, when n by 1 when , Complete the entire movement .
therefore , To solve the tower of Hanoi problem, a recursive function can be designed , Using recursion to realize the whole moving process of the disk , The problem solving process is a simulation of actual operation .
Four 、 Solutions
There are four common strategies to solve the Hanoi Tower problem :
- Loop sub goal , Also known as target recursion strategy : The idea is to move the largest pyramid to the column 3, First move the second largest pyramid to the column 2; And to move the next largest pyramid to the column 2, It is necessary to first move the pyramid one layer smaller than it to the column 3;… By analogy , Until you only need to move the top plate . This strategy is similar to computer recursion , It is an internally directed strategy , Subjects do not have to look at specific stimuli , Just keep your internal goals in mind , Then step by step loop execution , Until the problem is solved .
- Perceptual strategies : This strategy is the strategy of stimulating and guiding , According to the relationship between the situation and the goal , Remove the biggest obstacle at present , So as to achieve the goal step by step .
- Mode strategy : It is also the strategy of internal guidance , But it doesn't involve the goal , But to act according to certain rules . The general rule for solving the river tower is , When the total number of discs is odd , The smallest disc press 1->3->2->1->3->2 Move in order , When the total number is even , Press 1->2->3->1->2->3 Move in order .
- Mechanical memory strategies : This strategy is to memorize a series of steps to do the right thing , But you can't innovate , Not transferable .
5、 ... and 、 Code implementation
Here I take the first solution as an example to code
Sample code :
#include<iostream>
using namespace std;
int m=0; // Count the movement
void move(char A,int n,char C) // Moving operation
{
cout<<" The first "<<++m<<" Step ,"<<" Will be numbered as "<<n<<" The disc of "<<A<<" Move the first column to the "<<C<<" On the pillars "<<endl;
}
void hanoi(int n,char A,char B,char C) // Put the tower A Upper n A disc is moved according to the rules C On
{
if(n==1)
move(A,1,C);
else
{
hanoi(n-1,A,C,B); // take A The above number is 1 to n-1 Move your disc to B,C As an auxiliary tower
move(A,n,C); // Will be numbered as n The disc from A Move to C
hanoi(n-1,B,A,C); // take B The above number is 1 to n-1 Move your disc to C,A As an auxiliary tower
}
}
int main()
{
int n;
char a,b,c;
a='1';
b='2';
c='3';
cout<<" Please enter the number of discs on the initial first column :"<<endl;
cin>>n;
cout<<" The process of moving all the discs on the first column to the third column is :"<<endl;
hanoi(n,a,b,c);
return 0;
}
边栏推荐
- Photoshop layer related concepts layercomp layers move rotate duplicate layer compound layer
- [cloud based co creation] the "solution" of Digital Travel construction in Colleges and Universities
- C# 二维码生成、识别,去除白边、任意颜色
- 现在网上买股票开户身份证信息安全吗?
- 429-二叉树(108. 将有序数组转换为二叉搜索树、538. 把二叉搜索树转换为累加树、 106.从中序与后序遍历序列构造二叉树、235. 二叉搜索树的最近公共祖先)
- Garbage collector driving everything -- G1
- “我让这个世界更酷”2022华清远见研发产品发布会圆满成功
- 《第五项修炼》(The Fifth Discipline):学习型组织的艺术与实践
- 驾驭一切的垃圾收集器 -- G1
- NVIDIA Clara-AGX-Developer-Kit installation
猜你喜欢

基于STM32F103ZET6库函数蜂鸣器实验

华大单片机KEIL报错_WEAK的解决方案

The IPO of Yuchen Airlines was terminated: Guozheng was proposed to raise 500million yuan as the major shareholder

深度学习和神经网络的介绍

拥抱云原生:江苏移动订单中心实践

Solution of adding st-link to Huada MCU Keil

如何利用 RPA 实现自动化获客?

Bit.Store:熊市漫漫,稳定Staking产品或成主旋律

爬取国家法律法规数据库

金源高端IPO被终止:曾拟募资7.5亿 儒杉资产与溧阳产投是股东
随机推荐
Doctoral Dissertation of the University of Toronto - training efficiency and robustness in deep learning
2022年第一季度消费金融APP用户洞察——总数达4479万人
C# 二维码生成、识别,去除白边、任意颜色
金鱼哥RHCA回忆录:DO447管理项目和开展作业--创建作业模板并启动作业
(LC)46. 全排列
SQL Server - Window Function - 解决连续N条记录过滤问题
Bit. Store: long bear market, stable stacking products may become the main theme
现在网上买股票开户身份证信息安全吗?
【登录界面】
1025 PAT Ranking
谈谈线程安全
使用MySqlBulkLoader批量插入数据
基础数据类型和复杂数据类型
NVIDIA Clara-AGX-Developer-Kit installation
Four years of College for an ordinary graduate
Memoirs of actual combat: breaking the border from webshell
昱琛航空IPO被终止:曾拟募资5亿 郭峥为大股东
Character interception triplets of data warehouse: substrb, substr, substring
华大单片机KEIL报错_WEAK的解决方案
Cdga | what is the core of digital transformation in the transportation industry?