当前位置:网站首页>Tower of Hanoi II | tower of Hanoi 4 columns
Tower of Hanoi II | tower of Hanoi 4 columns
2022-07-26 10:03:00 【Alan_ Lowe】
Hanoi II| Hanoi 4 column
Problem Description
The classical Hanoi Tower problem often exists as a recursive classical example . Maybe someone doesn't know the story of the tower of Hanoi problem . The tower of Hanoi comes from a story in Indian legend , God made three diamond pillars when he created the world , On a column, they are stacked in order of size from bottom to top 64 A golden disk . God ordered Brahman to rearrange the disc on another post in order of size from below . And stipulate , You can't enlarge a disc on a small disc , Only one disc can be moved between the three pillars at a time . There is a prophecy that , When this is done, the universe will be destroyed in a flash . Some people believe that Brahmans are still moving the disc all the time . Okay , Of course, this legend is not believable , Nowadays, Hanoi Tower exists more as a toy .Gardon I received a Hanoi Tower toy as a birthday gift .
Gardon He is afraid of trouble ( Okay , Is a lazy person ), It is obvious that 64 It's difficult to move the plates one by one until all the plates reach the third pillar , therefore Gardon Decided to make a small mistake , He found another exactly the same post , Move all the dishes to the third pillar faster through this pillar . The following question is : When Gardon Used in a game N When you have a plate , How many times does he need to move them to the third pillar ? Obviously , When there is no fourth pillar , The solution to the problem is 2^N-1, But now with the help of this pillar , How much should it be ?
Input
Contains multiple sets of data , One line per data , It's the number of plates N(1<=N<=64).
Output
For each set of data , Output a number , The minimum number of moves required to reach the target .
Sample Input
1
3
12
Sample Output
1
5
81
Be careful
If this topic doesn't open unsigned long long Will always be wa.
#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long // If this topic doesn't open ull You can't make it (tu...)
int f3[70],f4[70];
void init(){
f3[0] = 0;
for (int i = 1; i <= 64; ++i) {
f3[i] = 2 * f3[i - 1] + 1;
}
memset(f4,0x7f,sizeof f4);f4[0] = 0;
for (int i = 1; i <= 64; ++i) {
for (int j = 0; j <= i; ++j) {
f4[i] = min(f4[i],2 * f4[j] + f3[i - j]);
}
}
}
signed main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
init();
int n;
while (cin>>n){
cout<<f4[n]<<"\n";
}
return 0;
}
边栏推荐
- Session based recommendations with recurrent neural networks
- Interview shock 68: why does TCP need three handshakes?
- 解释一下自动装箱和自动拆箱?
- Leetcode 504. 七进制数
- Study notes of the second week of sophomore year
- Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
- copyTo
- The fourth week of summer vacation
- Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market
- Applet record
猜你喜欢
2022年中科磐云——服务器内部信息获取 解析flag
Basic usage of protobuf
Sqoop [environment setup 01] CentOS Linux release 7.5 installation configuration sqoop-1.4.7 resolve warnings and verify (attach sqoop 1 + sqoop 2 Latest installation package +mysql driver package res
数通基础-二层交换原理
Mysql5.7.25 master-slave replication (one-way)
阿里云技术专家郝晨栋:云上可观测能力——问题的发现与定位实践
Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning
数通基础-网络基础知识
Keeping alive to realize MySQL automatic failover
Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market
随机推荐
数通基础-网络基础知识
Vs2019 configuring opencv
Sqoop [put it into practice 02] sqoop latest version full database import + data filtering + field type support description and example code (query parameter and field type forced conversion)
JS continuous assignment operation
(2) Hand eye calibration of face scanner and manipulator (eye out of hand: nine point calibration)
Rocky basic exercise -shell script 2
copyTo
The use of MySQL in nodejs
In Net 6.0
Applet record
Interpretation of the standard of software programming level examination for teenagers_ second level
Mqtt x cli officially released: powerful and easy-to-use mqtt 5.0 command line tool
SQL优化的魅力!从 30248s 到 0.001s
Usage of the formatter attribute of El table
Explain automatic packing and unpacking?
B站这个视频我是跪着看完的
Write a script that can run in Bash / shell and PowerShell
在Blazor 中自定义权限验证
JS judge the data types object.prototype.tostring.call and typeof
网易云UI模仿--&gt;侧边栏