当前位置:网站首页>2.6 using recursion and stack - [tower of Hanoi problem]
2.6 using recursion and stack - [tower of Hanoi problem]
2022-07-02 12:33:00 【I boiled the night white_】
List of articles
Title Description
The tower of Hanoi is an ancient mathematical problem
There are three poles A,B,C.A There's... On the pole n individual (n>1) Perforated disc , The size of the disc decreases from bottom to top .
All discs are required to be moved to C rod :
- Only one disc can be moved at a time ;
- The big plate can't be stacked on the small one .
Tips :
The disc can be temporarily placed in B rod , You can also start from A The disc from which the rod is removed moves back A rod , But both must follow the above two rules .
ask : How to move ? At least how many times to move ?
Input description
a line , contain 2 A positive integer , One is N, Indicates the number of disks to be moved ; One is M, Represents the number of the least moving steps M Step .
Output description
common 2 That's ok .
The output format of the first line is :#No:a->b, It means the first one M Step specific moving method , among No It means the first one M The number of the moving plate (N The plates are numbered from top to bottom 1 To n), It means the first one M The next step is to No Plate from a The rod moves to b rod (a and b All values are (A,B、C]).
The first 2 Line outputs an integer , Represents the minimum number of moving steps .
I/o sample
Input :
3 2
Output :
#2: A->B
7
The final code
1. c/c++ 【 recursive 】
#include<bits/stdc++.h>
using namespace std;
int sum = 0, m;
void hanoi(char x,char y,char z,int n)
{
//m It's the first step ,n It's the number of plates
if(n==1)
{
sum++;
if(sum==m) cout<<"#"<<n<<": "<<x<<"->"<<z<<endl;
}
else
{
hanoi(x,z,y,n-1);
sum++;
if(sum==m) cout<<"#"<<n<<": "<<x<<"->"<<z<<endl;
hanoi(y,x,z,n-1);
}
}
int main()
{
int n;
cin>>n>>m;
hanoi('A','B','C',n);
cout<<sum<<endl;
return 0;
}
1. c/c++ 【 Stack , Not recommended 】
#include<bits/stdc++.h>
using namespace std;
const int N = 30;
int sum = 0, m;
struct mystack{
int a[N]; // Store stack elements
int t = -1; // The top of the stack
void push(int x){
a[++t] = x; } // hold x Feed stack
int top() {
return a[t]; } // Back to top of stack element
void pop() {
t--; } // Pop up the top of the stack
int empty() {
return t==0?1:0;} // return 1 Said empty
}st[4]; // Define three columns , no need st[0]
void move(int x, int y,int n){
// Moving disc
int element = st[x].top(); // Slave pole x Take out the top disc and put it on the pole y On
st[x].pop();
st[y].push(element);
sum++;
char a,b; // For printing
if(x==1) a='A'; if(x==2) a='B'; if(x==3) a='C';
if(y==1) b='A'; if(y==2) b='B'; if(y==3) b='C';
if(sum == m) cout<<"#"<<n<<": "<<a<<"->"<<b<<endl;
}
void hanoi(int n, int x, int y, int z){
if(n==1) move(x,z,n);
else{
hanoi(n-1, x, z, y);
move(x,z,n);
hanoi(n-1, y, x, z);
}
}
int main(){
int n; cin>>n>>m;
for(int i=n;i>=1;i--) // The initial state : In the 1 Add n Disc
st[1].push(i);
hanoi(n,1,2,3);
cout<<sum<<endl;
return 0;
}
2. java
3. python
Process understanding
边栏推荐
- Deep Copy Event bus
- The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night
- Initial JDBC programming
- 初始JDBC 编程
- 计算二叉树的最大路径和
- 怎样写一篇赏心悦目的英文数学论文
- Is the neural network (pinn) with embedded physical knowledge a pit?
- AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning
- Error in kubeadm join: [error port-10250]: port 10250 is in use [error fileavailable--etc kubernetes PKI
- [FFH] little bear driver calling process (take calling LED light driver as an example)
猜你喜欢

寻找二叉树中任意两个数的公共祖先

Record the range of data that MySQL update will lock

CDA数据分析——AARRR增长模型的介绍、使用

Heap (priority queue)

甜心教主:王心凌

Sweetheart leader: Wang Xinling

测试左移和右移
![[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol](/img/13/9002244555ebe8a61660c2506993fa.png)
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol

CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节

Tas (file d'attente prioritaire)
随机推荐
drools执行完某个规则后终止别的规则执行
H5 to app
甜心教主:王心凌
Map and set
String palindrome hash template question o (1) judge whether the string is palindrome
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
Docker compose configuration mysql, redis, mongodb
Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
Adding database driver to sqoop of cdh6
arcgis js 4. Add pictures to x map
Sweetheart leader: Wang Xinling
Leetcode209 subarray with the smallest length
LeetCode—剑指 Offer 59 - I、59 - II
Drools dynamically add, modify, and delete rules
Docker-compose配置Mysql,Redis,MongoDB
MySQL与PostgreSQL抓取慢sql的方法
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
From scratch, develop a web office suite (3): mouse events
Natural language processing series (I) -- RNN Foundation

