多柱汉诺塔问题。

  引用自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的更多相关文章

  1. SGU 202 The Towers of Hanoi Revisited (DP+递归)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意 :n个圆盘,m个柱子的汉诺塔输出步骤. ht ...

  2. 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 ...

  3. ZOJ-2338 The Towers of Hanoi Revisited 输出汉诺塔的最优解移动过程

    题意:给定N(1<= N <=64)个盘子和M(4<= M <= 65)根柱子,问把N个盘子从1号柱子移动到M号柱子所需要的最少步数,并且输出移动过程. 分析:设f[i][j] ...

  4. The Towers of Hanoi Revisited---(多柱汉诺塔)

    Description You all must know the puzzle named "The Towers of Hanoi". The puzzle has three ...

  5. [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 ...

  6. 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 ...

  7. POJ 1958 Strange Towers of Hanoi

    Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3784 Accepted: 23 ...

  8. POJ-1958 Strange Towers of Hanoi(线性动规)

    Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 2677 Accepted: 17 ...

  9. ural 2029 Towers of Hanoi Strike Back (数学找规律)

    ural 2029 Towers of Hanoi Strike Back 链接:http://acm.timus.ru/problem.aspx?space=1&num=2029 题意:汉诺 ...

随机推荐

  1. EurekaServer集群配置

    一.程序配置 1.pom添加依赖: <dependency> <groupId>org.springframework.cloud</groupId> <ar ...

  2. Consul 入门(二)

    KV 存储 通过命令行操作 $ consul kv put hello world # 设置数据 Success! Data written to: hello $ consul kv get hel ...

  3. matlab特殊符号表

    Character Sequence Symbol Character Sequence Symbol Character Sequence Symbol \alpha α \upsilon υ \s ...

  4. XOR and Favorite Number (莫对算法)

    E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  5. UVALive - 5713 最小生成树

    题意: 秦始皇修路,已知n个城市的坐标以及该城市的人口数,修路的费用是两个城市之间的欧几里得距离,其中可以有一条路不用花费代价但是要求这条路连接的两个城市的人口之和A/B尽量大,其中B是修路的总费用. ...

  6. Http字段含义

    转载自:http://blog.csdn.net/sand_ant/article/details/10503579 一.request请求Header简介 Accept:--客户机支持的类型 Acc ...

  7. JS常见的算法

    原文链接:Jack Pu's Blog 虽说我们很多时候前端很少有机会接触到算法.实际上学习数据结构与算法对于工程师去理解和分析问题都是有帮助的.如果将来当我们面对较为复杂的问题,这些基础知识的积累可 ...

  8. pyttsx3 winsound win32api.MessageBox使用案例

    import requests,time from lxml import etree import win32api,win32con import winsound import pyttsx3 ...

  9. 持续集成之Jenkins安装部署

    1.系统环境和安装java环境 [root@devops ~]# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) 安装java ...

  10. mysql 多列唯一索引在事务中select for update是不是行锁?

    在表中有这么一索引 UNIQUE KEY `customer_id` (`customer_id`,`item_id`,`ref_id`) 问1. 这种多列唯一索引在事务中select for upd ...