当前位置:网站首页>Strange Towers of Hanoi|汉诺塔4柱问题
Strange Towers of Hanoi|汉诺塔4柱问题
2022-07-26 09:58:00 【Alan_Lowe】
Strange Towers of Hanoi
Background
Charlie Darkbrown sits in another one of those boring Computer Science lessons: At the moment the teacher just explains the standard Tower of Hanoi problem, which bores Charlie to death!
Problem
链接:https://ac.nowcoder.com/acm/problem/50921
来源:牛客网
So your task is to write a program that calculates the smallest number of disk moves necessary to move all the disks from tower A to C."
Charlie: “This is incredibly boring—everybody knows that this can be solved using a simple recursion.I deny to code something as simple as this!”
The teacher sighs: “Well, Charlie, let’s think about something for you to do: For you there is a fourth tower D. Calculate the smallest number of disk moves to move all the disks from tower A to tower D using all four towers.”
Charlie looks irritated: "Urgh. . . Well, I don’t know an optimal algorithm for four towers. . . "
Problem
So the real problem is that problem solving does not belong to the things Charlie is good at. Actually, the only thing Charlie is really good at is “sitting next to someone who can do the job”. And now guess what — exactly! It is you who is sitting next to Charlie, and he is already glaring at you.
Luckily, you know that the following algorithm works for n <= 12: At first k >= 1 disks on tower A are fixed and the remaining n-k disks are moved from tower A to tower B using the algorithm for four towers.Then the remaining k disks from tower A are moved to tower D using the algorithm for three towers. At last the n - k disks from tower B are moved to tower D again using the algorithm for four towers (and thereby not moving any of the k disks already on tower D). Do this for all k 2 ∈{1, … , n} and find the k with the minimal number of moves.
So for n = 3 and k = 2 you would first move 1 (3-2) disk from tower A to tower B using the algorithm for four towers (one move). Then you would move the remaining two disks from tower A to tower D using the algorithm for three towers (three moves). And the last step would be to move the disk from tower B to tower D using again the algorithm for four towers (another move). Thus the solution for n = 3 and k = 2 is 5 moves. To be sure that this really is the best solution for n = 3 you need to check the other possible values 1 and 3 for k. (But, by the way, 5 is optimal. . . )
输入描述:
There is no input.
输出描述:
For each n (1 <= n <= 12) print a single line containing the minimum number of moves to solve the problem for four towers and n disks.
#include<bits/stdc++.h>
using namespace std;
#define int long long
int f3[20],f4[20];
signed main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
f3[0] = 0;
for (int i = 1; i <= 12; ++i) {
f3[i] = 2 * f3[i - 1] + 1;
}
memset(f4,0x3f,sizeof f4);f4[0] = 0;
for (int i = 1; i <= 12; ++i) {
for (int j = 0; j <= i; ++j) {
f4[i] = min(f4[i],2 * f4[j] + f3[i - j]);
}
cout<<f4[i]<<"\n";
}
return 0;
}
边栏推荐
- AR model in MATLAB for short-term traffic flow prediction
- Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market
- Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken
- MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
- JS 一行代码 获取数组最大值与最小值
- (1) Hand eye calibration of face scanner and manipulator (eye on hand)
- 服务发现原理分析与源码解读
- 在.NET 6.0中配置WebHostBuilder
- Due to fierce competition in the new market, China Mobile was forced to launch a restrictive ultra-low price 5g package
- Customize permission validation in blazor
猜你喜欢

Azkaban【基础知识 01】核心概念+特点+Web界面+架构+Job类型(一篇即可入门Azkaban工作流调度系统)

Solve NPM -v sudden failure and no response

服务器内存故障预测居然可以这样做!

Production of a-modal drag function in antui

【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛

30分钟彻底弄懂 synchronized 锁升级过程

Leetcode 504. Hex number

Leetcode 504. 七进制数

论文笔记(SESSION-BASED RECOMMENDATIONS WITHRECURRENT NEURAL NETWORKS)

2021年山东省中职组“网络空间安全”B模块windows渗透(解析)
随机推荐
Study notes of the fifth week of sophomore year
Meeting OA project (III) -- my meeting (meeting seating and submission for approval)
R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐、align legend title to middle of legend)
Li Kou - binary tree pruning
30分钟彻底弄懂 synchronized 锁升级过程
【信息系统项目管理师】初见高项系列精华汇总
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)
In the same CONDA environment, install pytroch first and then tensorflow
Study notes at the end of summer vacation
The diagram of user login verification process is well written!
Spolicy request case
Solve the problem of storing cookies in IE7 & IE8
反射机制的原理是什么?
2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
Node memory overflow and V8 garbage collection mechanism
Usage of the formatter attribute of El table
Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
论文笔记(SESSION-BASED RECOMMENDATIONS WITHRECURRENT NEURAL NETWORKS)
2021年山东省中职组“网络空间安全”B模块windows渗透(解析)
[datawhale] [machine learning] Diabetes genetic risk detection challenge