当前位置:网站首页>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;
}
边栏推荐
- 网络传输是怎么工作的 -- 详解 OSI 模型
- 使用MySqlBulkLoader批量插入数据
- 华大单片机KEIL添加ST-LINK解决方法
- 经纬度分析
- Doctoral Dissertation of the University of Toronto - training efficiency and robustness in deep learning
- International School of Digital Economics, South China Institute of technology 𞓜 unified Bert for few shot natural language understanding
- 过关斩将,擒“指针”(下)
- Function key input experiment based on stm32f103zet6 Library
- 运算符的基础知识
- Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
猜你喜欢

一种朴素的消失点计算方法

工作流自动化 低代码是关键

Running lantern experiment based on stm32f103zet6 library function

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

数仓的字符截取三胞胎:substrb、substr、substring

国际数字经济学院、华南理工 | Unified BERT for Few-shot Natural Language Understanding(用于小样本自然语言理解的统一BERT)

MySQL初学者福利

数组练习 后续补充

What is ICMP? What is the relationship between Ping and ICMP?

華大單片機KEIL報錯_WEAK的解决方案
随机推荐
Four years of College for an ordinary graduate
驾驭一切的垃圾收集器 -- G1
作为软件工程师,给年轻时的自己的建议(下)
Photoshop layer related concepts layercomp layers move rotate duplicate layer compound layer
经纬度分析
如何利用 RPA 实现自动化获客?
工作流自动化 低代码是关键
基础数据类型和复杂数据类型
Erreur Keil de Huada Single Chip Computer La solution de Weak
Introduction to deep learning and neural networks
One to one relationship
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
Code and principle of RANSAC
【建议收藏】ABAP随笔-EXCEL-4-批量导入-推荐
What is ssr/ssg/isr? How do I host them on AWS?
Redis 原理 - String
華大單片機KEIL報錯_WEAK的解决方案
一种朴素的消失点计算方法
Informatics Olympiad 1333: [example 2-2] blah data set | openjudge noi 3.4 2729:blah data set
基于STM32F103ZET6库函数蜂鸣器实验