当前位置:网站首页>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
边栏推荐
- Leetcode14 longest public prefix
- CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
- When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
- Writing method of then part in drools
- 中国交通标志检测数据集
- 浏览器存储方案
- Deep Copy Event bus
- Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
- Leetcode739 每日温度
- 排序---
猜你喜欢

Jenkins用户权限管理

Tas (file d'attente prioritaire)

Writing method of then part in drools

使用Sqoop把ADS层数据导出到MySQL

记录一下MySql update会锁定哪些范围的数据

China traffic sign detection data set

堆(优先级队列)

Differences between nodes and sharding in ES cluster

MySQL indexes and transactions

Sparkcontext: error initializing sparkcontext solution
随机推荐
In development, why do you find someone who is paid more than you but doesn't write any code?
JZ63 股票的最大利润
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
LeetCode—剑指 Offer 37、38
Introduction to CPU instruction set
Drools terminates the execution of other rules after executing one rule
Deep understanding of P-R curve, ROC and AUC
CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
String palindrome hash template question o (1) judge whether the string is palindrome
CDA数据分析——AARRR增长模型的介绍、使用
高性能纠删码编码
Drools dynamically add, modify, and delete rules
防抖 节流
WSL 2 will not be installed yet? It's enough to read this article
5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
kubeadm join时出现错误:[ERROR Port-10250]: Port 10250 is in use [ERROR FileAvailable--etc-kubernetes-pki
SparkContext: Error initializing SparkContext解决方法
Brush questions --- binary tree --2
LeetCode—剑指 Offer 59 - I、59 - II
[I'm a mound pytorch tutorial] learning notes

