当前位置:网站首页>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
边栏推荐
- Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
- Performance tuning project case
- 计算二叉树的最大路径和
- Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
- Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately
- From scratch, develop a web office suite (3): mouse events
- LeetCode—剑指 Offer 51. 数组中的逆序对
- 倍增 LCA(最近公共祖先)
- Codeforces 771-div2 C (trouble, permutation is not very good)
- arcgis js 4. Add pictures to x map
猜你喜欢
[C language] convert decimal numbers to binary numbers
Sweetheart leader: Wang Xinling
Simple understanding of ThreadLocal
Writing method of then part in drools
MySQL indexes and transactions
测试左移和右移
Test shift left and right
Deep understanding of P-R curve, ROC and AUC
Deep copy event bus
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
随机推荐
[C language] Yang Hui triangle, customize the number of lines of the triangle
mysql索引和事务
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
Jenkins user rights management
drools决策表的简单使用
Leetcode - Sword finger offer 59 - I, 59 - II
MySQL与PostgreSQL抓取慢sql的方法
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
深拷贝 事件总线
防抖 节流
CDA数据分析——Excel数据处理的常见知识点归纳
计算二叉树的最大路径和
Find the common ancestor of any two numbers in a binary tree
mysql数据库基础
分布式机器学习框架与高维实时推荐系统
Go学习笔记—多线程
CDA数据分析——AARRR增长模型的介绍、使用
Calculate the maximum path sum of binary tree
Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately