当前位置:网站首页>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;
}
边栏推荐
- Server memory failure prediction can actually do this!
- 【荧光字效果】
- 【有奖提问】向图灵奖得主、贝叶斯网络之父 Judea Pearl 提问啦
- 2022 zhongkepan cloud - server internal information acquisition and analysis flag
- Flask框架初学-03-模板
- Azkaban【基础知识 01】核心概念+特点+Web界面+架构+Job类型(一篇即可入门Azkaban工作流调度系统)
- 点赞,《新程序员》电子书限时免费领啦!
- 【信息系统项目管理师】初见高项系列精华汇总
- Opencv image processing
- Time series anomaly detection
猜你喜欢

Node memory overflow and V8 garbage collection mechanism

Interview shock 68: why does TCP need three handshakes?

面试突击68:为什么 TCP 需要 3 次握手?

Principle analysis and source code interpretation of service discovery

苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收

Solve NPM -v sudden failure and no response

Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning

Leetcode 504. Hex number

Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice

数通基础-STP原理
随机推荐
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
阿里云技术专家郝晨栋:云上可观测能力——问题的发现与定位实践
【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛
What is the principle of reflection mechanism?
The diagram of user login verification process is well written!
Meeting OA project (III) -- my meeting (meeting seating and submission for approval)
Interpretation of the standard of software programming level examination for teenagers_ second level
Usage of the formatter attribute of El table
2022年中科磐云——服务器内部信息获取 解析flag
Principle analysis and source code interpretation of service discovery
Nodejs service background execution (forever)
Uniapp common error [wxml file compilation error]./pages/home/home Wxml and using MySQL front provided by phpstudy to establish an independent MySQL database and a detailed tutorial for independent da
Uniapp "no mobile phone or simulator detected, please try again later" and uniapp custom components and communication
QT handy notes (III) use qtcharts to draw a line chart in VS
Flask框架初学-04-flask蓝图及代码抽离
Gauss elimination solves the inverse of matrix (Gauss)
[information system project manager] summary of essence of high-level series for the first time
Vectortilelayer replacement style
Study notes of the first week of sophomore year
Write a script that can run in Bash / shell and PowerShell