多柱汉诺塔问题。

  引用自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. ACE线程管理机制-面向对象的线程类ACE_Task

    转载于:http://www.cnblogs.com/TianFang/archive/2006/12/05/583231.html 我们在前一章中使用ACE_Thread包装时,你一定已经注意到了一 ...

  2. 宽度搜索(BFS)中求最短路径问题理解记录

    借助ACM1242题深入理解迷宫类最短路径搜索并记录路径长度的问题及解决方法:这是初次接触优先队列,尤其是不知道该怎样去记忆在结构体重自定义大小比较的符号方向,很容易混淆符号向哪是从大到小排列,向哪是 ...

  3. 使用Android Studio调试UiAutomator过程中遇到的问题

    声明: 这里纪录了个人学习和使用Android Studio调试UiAutomator过程中遇到遇到的问题,不定时进行更新,欢迎一起交流学习 1.Excution faild for task ‘:a ...

  4. 单例 ------ JAVA实现

    单例:只能实例化一个对象,使用场景比如打印机. 最推荐的是采用饿汉式:双重校验锁用到了大量的语法,不能保证这些语法在所用场合一定没问题,所以不是很推荐:总之简单的才是最好的,就饿汉式!!! C++ 创 ...

  5. [LeetCode] 10. Regular Expression Matching ☆☆☆☆☆

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  6. Weblogic 9.2 启动时报错 javax.xml.namespace.QName

    启动Weblogic 时会报错.javax.xml.namespace.QName; local class incompatible: stream classdesc serialVersionU ...

  7. Codeforces Round #380 (Div. 2)/729B Spotlights 水题

    Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which act ...

  8. 【设计模式】 模式PK:命令模式VS策略模式

    1.概述 命令模式和策略模式的类图确实很相似,只是命令模式多了一个接收者(Receiver)角色.它们虽然同为行为类模式,但是两者的区别还是很明显的.策略模式的意图是封装算法,它认为“算法”已经是一个 ...

  9. Redux Concepts

    Redux解决数据通信复杂问题. Store 存储数据的地方,一个应用只有一个Store. State Store对象包含所有数据. Action 一个对象,表示View的变化. Action Cre ...

  10. Chrome浏览器启动页被360导航篡改解决方法

    右键Chrome浏览器快捷方式,选择“属性”,在“目标”的结尾处有添加的网址,删了即可. 2 如果在结尾处没有任何网址,可以添加“ -nohome”,这样下次启动时,就会打开一个空白页,也就不会打开被 ...