当前位置:网站首页>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;
}
边栏推荐
- [cloud based co creation] the "solution" of Digital Travel construction in Colleges and Universities
- GIS remote sensing R language learning see here
- Market status and development prospect forecast of global 4-methyl-2-pentanone industry in 2022
- Market status and development prospect forecast of global active quality control air sampler industry in 2022
- Cucumber自动化测试框架使用
- 经纬度分析
- 教你打印自己的日志 -- 如何自定义 log4j2 各组件
- Solution of adding st-link to Huada MCU Keil
- Gartner聚焦中国低代码发展 UniPro如何践行“差异化”
- 可靠的分布式锁 RedLock 与 redisson 的实现
猜你喜欢

从感知机到前馈神经网络的数学推导

DFS and BFS simple principle

Vs code runs "yarn run dev" and reports "yarn": the file XXX cannot be loaded

今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献

308. 二维区域和检索 - 可变 线段树/哈希

Embracing cloud Nativity: Practice of Jiangsu Mobile order center

A simple calculation method of vanishing point

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

Bit. Store: long bear market, stable stacking products may become the main theme

Blink SQL内置函数大全
随机推荐
Photoshop-图层相关概念-LayerComp-Layers-移动旋转复制图层-复合图层
现在网上买股票开户身份证信息安全吗?
你知道 log4j2 各项配置的全部含义吗?带你了解 log4j2 的全部组件
Making single test so simple -- initial experience of Spock framework
One to one relationship
金鱼哥RHCA回忆录:DO447管理项目和开展作业--创建作业模板并启动作业
shell脚本常用命令(三)
Workflow automation low code is the key
数组练习 后续补充
GIS遥感R语言学习看这里
CDGA|交通行业做好数字化转型的核心是什么?
使用logrotate对宝塔的网站日志进行自动切割
一对一关系
基于STM32F103ZET6库函数按键输入实验
1023 Have Fun with Numbers
How to encapsulate and call a library
【云驻共创】 什么是信息化?什么是数字化?这两者有什么联系和区别?
指针和结构体
拥抱云原生:江苏移动订单中心实践
NVIDIA Clara-AGX-Developer-Kit installation