SGU 202. The Towers of Hanoi Revisited
多柱汉诺塔问题。
引用自wiki百科
多塔汉诺塔问题
- 在有3个柱子时,所需步数的公式较简单,但对于4个以上柱子的汉诺塔尚未得到通用公式,但有一递归公式(未得到证明,但目前为止没有找到反例):
- 令
为在有k个柱子时,移动n个圆盘到另一柱子上需要的步数,则:
- 对于任何移动方法,必定会先将
个圆盘移动到一个中间柱子上,再将第n到第n-m个圆盘通过剩下的k-1个柱子移到目标柱子上,最后将m个在中间柱子上的圆盘移动到目标柱子上。这样所需的操作步数为
。
- 进行最优化,易得:
。
#include <bits/stdc++.h>
#define rep(_i, _n) for(int _i = 1; _i <= _n; ++_i)
typedef long long LL;
typedef double DB;
const int inf = (INT_MAX / ) - ; using namespace std;
const int maxn = + ;
int n, m;
int f[maxn][maxn], pos[maxn][maxn];
void dfs(int a, int b) {
if(f[a][b] != -) return ;
f[a][b] = inf;
if(b < ) return ;
rep(r, a - ) {
dfs(r, b);
dfs(a - r, b - );
int tmp = f[r][b] * + f[a - r][b - ];
if(tmp < f[a][b]) {
f[a][b] = tmp;
pos[a][b] = r;
}
}
}
int tower[maxn][maxn], num[maxn]; void print(int s, int t, int a, int b) {
if(a == ) {
printf("move %d from %d to %d ", tower[s][num[s]], s, t);
if(num[t] != ) printf("atop %d", tower[t][num[t]]);
puts("");
tower[t][++num[t]] = tower[s][num[s]--];
return ;
}
rep(i, m) if(i != s && i != t) {
if(tower[i][num[i]] > tower[s][num[s] - pos[a][b] + ]) {
print(s, i, pos[a][b], b);
print(s, t, a - pos[a][b], b - );
print(i, t, pos[a][b], b);
return ;
}
}
} int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin), freopen("data.out", "w", stdout);
#endif cin >> n >> m;
memset(f, -, sizeof f);
rep(i, m) f[][i] = ;
dfs(n, m);
cout << f[n][m] << '\n';
for(int i = n; < i; --i) tower[][++num[]] = i;
rep(i, m) tower[i][] = inf;
print(, m, n, m); return ;
}
SGU 202. The Towers of Hanoi Revisited的更多相关文章
- SGU 202 The Towers of Hanoi Revisited (DP+递归)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意 :n个圆盘,m个柱子的汉诺塔输出步骤. ht ...
- zoj 2338 The Towers of Hanoi Revisited
The Towers of Hanoi Revisited Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge You all mus ...
- ZOJ-2338 The Towers of Hanoi Revisited 输出汉诺塔的最优解移动过程
题意:给定N(1<= N <=64)个盘子和M(4<= M <= 65)根柱子,问把N个盘子从1号柱子移动到M号柱子所需要的最少步数,并且输出移动过程. 分析:设f[i][j] ...
- The Towers of Hanoi Revisited---(多柱汉诺塔)
Description You all must know the puzzle named "The Towers of Hanoi". The puzzle has three ...
- [CareerCup] 3.4 Towers of Hanoi 汉诺塔
3.4 In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of different sizes ...
- POJ 1958 Strange Towers of Hanoi 解题报告
Strange Towers of Hanoi 大体意思是要求\(n\)盘4的的hanoi tower问题. 总所周知,\(n\)盘3塔有递推公式\(d[i]=dp[i-1]*2+1\) 令\(f[i ...
- POJ 1958 Strange Towers of Hanoi
Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3784 Accepted: 23 ...
- POJ-1958 Strange Towers of Hanoi(线性动规)
Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 2677 Accepted: 17 ...
- ural 2029 Towers of Hanoi Strike Back (数学找规律)
ural 2029 Towers of Hanoi Strike Back 链接:http://acm.timus.ru/problem.aspx?space=1&num=2029 题意:汉诺 ...
随机推荐
- dubbox管理中心
当服务多了,不好管理,可以用管理中心来查看. 管理中心就是一个 web项目(一个war包),可以部署在tomcat下面就能运行了. 用户名密码都是root 每一条服务点开后都可以看生产者和消费者.
- Codeforces Round #514 (Div. 2) C. Sequence Transformation(递归)
C. Sequence Transformation 题目链接:https://codeforces.com/contest/1059/problem/C 题意: 现在有1~n共n个数,然后执行下面操 ...
- 轮廓问题/Outline Problem
--------------------------------------------------- //已发布改进后的轮廓问题算法:http://www.cnblogs.com/andyzeng/ ...
- MySql 利用函数 查询所有子节点
前提:mysql 函数 find_in_set(str,strlist), cast(value as type) 一.find_in_set(str,strlist):如果字符串str是在的 ...
- emoji表情处理研究
http://blog.csdn.net/qdkfriend/article/details/7576524
- Independence.
It's not giving up, it's letting go, and moving to a better place. I will survive and be the one who ...
- Eclipse代码报错提示: the import java.util cannot be resolve,怎么解决?
显示 the import java.util cannot be resolve,如何解决?我在使用eclipse的时候, 好像无意中更改了安装位置(workspace),现在所有的包都显示无法导入 ...
- 【BZOJ4818】【SDOI2017】序列计数 [矩阵乘法][DP]
序列计数 Time Limit: 30 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Alice想要得到一个长度为n的序 ...
- hdu5828 Rikka with Sequence
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5828 [题解] 考虑bzoj3211 花神游历各国,只是多了区间加操作. 考虑上题写法,区间全为1打标记 ...
- 【BZOJ】1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
[算法]模拟 #include<cstdio> #include<algorithm> using namespace std; ,maxm=; int a[maxn],A[m ...
为在有k个柱子时,移动n个圆盘到另一柱子上需要的步数,则:
个圆盘移动到一个中间柱子上,再将第n到第n-m个圆盘通过剩下的k-1个柱子移到目标柱子上,最后将m个在中间柱子上的圆盘移动到目标柱子上。这样所需的操作步数为
。
。