转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove

题意 :n个圆盘,m个柱子的汉诺塔输出步骤。

http://acm.sgu.ru/problem.php?contest=0&problem=202

经典的递归问题,见具体数学第一章

先DP求出最短步骤,并记录路径。

然后 递归输出。

import java.util.*;
import java.io.*;
import java.math.*;
public class Solution {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(in, out);
out.close();
}
}
class Task{
static int dp [][] = new int [70][70];
static int pre[][] = new int [70][70];
static int inf = 100000009;
static int N,M;
Stack s[] = new Stack [70];
int dfs(int n,int m){
if(dp[n][m]!=-1)
return dp[n][m];
dp[n][m]=inf;
for(int i=1;i<n;i++){
int t=dfs(i,m)*2+dfs(n-i,m-1);
if(t<dp[n][m]){
dp[n][m]=t;
pre[n][m]=i;
}
}
return dp[n][m];
}
void move(int u,int v){
System.out.print("move " + s[u].peek() + " from " + u + " to " + v);
if(!s[v].empty())
System.out.print(" atop " + s[v].peek());
s[v].push(s[u].pop());
System.out.println();
}
void gao(int u,int v,int n,int m){
if(n==1){
move(u,v);
return ;
}
if(s[u].size()<pre[n][m]) return ;
for(int mid=1;mid<=M;mid++){
if(mid==u||mid==v) continue;
if(s[mid].empty()||((int)(s[mid].peek())>(int)(s[u].elementAt(s[u].size()-pre[n][m])))){
gao(u,mid,pre[n][m],m);
gao(u,v,n-pre[n][m],m-1);
gao(mid,v,pre[n][m],m);
return ;
}
}
}
void solve(InputReader in,PrintWriter out){
N=in.nextInt();M=in.nextInt();
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++)
dp[i][j]=-1;
for(int i=1;i<=N;i++)
dp[i][1]=dp[i][2]=inf;
dp[1][1]=0;
for(int i=2;i<=M;i++)
dp[1][i]=1;
dfs(N,M);
System.out.println(dp[N][M]);
for(int i=1;i<=M;i++)
s[i]=new Stack();
for(int i=N;i>=1;i--)
s[1].push(i);
gao(1,M,N,M);
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer; public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
} public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}

SGU 202 The Towers of Hanoi Revisited (DP+递归)的更多相关文章

  1. SGU 202. The Towers of Hanoi Revisited

    多柱汉诺塔问题. 引用自wiki百科 多塔汉诺塔问题 在有3个柱子时,所需步数的公式较简单,但对于4个以上柱子的汉诺塔尚未得到通用公式,但有一递归公式(未得到证明,但目前为止没有找到反例): 令为在有 ...

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

  5. POJ 1958 Strange Towers of Hanoi

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

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

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

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

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

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

  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. Windows主机和Linux虚拟机之间传输文件

    如果使用VirtualBox的增强功能, 可以实现两者之间文件相互拖拽. 但某些情况下, 比如增强功能安装遇到难以解决的问题, 或者Linux版本为server版本(例如Ubuntu Server发行 ...

  2. 【转】说说JSON和JSONP,也许你会豁然开朗,含jQuery用例

    由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Socket通讯 ...

  3. jquery validationEngine 使用ajax验证不通过也提交表单

    转自 http://mylfd.iteye.com/blog/2007227 validationEngine给我们为前端的表单验证减少了很大的工作量.大部分情况我们使用validationEngin ...

  4. 转载[WampServer下使用多端口访问]

    作者:韩子迟 原文链接:http://www.cnblogs.com/zichi/p/4589142.html 注意点:www和www2都需要安装服务: 在C:\wamp\bin\apache\Apa ...

  5. ndk 编译 boost 库,支持serialization

    Boost库是一个可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之一. Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容.在C+ ...

  6. JSTL与EL常用标签(转)

    JSTL与EL EL相关概念 JSTL一般要配合EL表达式一起使用,来实现在jsp中不出现java代码段.所以我们先来学习EL表达式 EL主要用于查找作用域中的数据,然后对它们执行简单操作:它不是编程 ...

  7. (Problem 39)Integer right triangles

    If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exact ...

  8. cocos2dx 字体

    有些时候需要在界面上显示些文字,自然涉及到字体的问题 显示文字使用CCLabelTTF即可,创建方法是 CCLabelTTF(const char* text, const char* font, i ...

  9. Codeforces Round#1

    A. Theatre Square 题目大意:有一个长宽为m和n的广场,用边长为a的正方形去铺盖,问铺满最少需要多少正方形 题解:题目分解为用长度为a的线条分别去覆盖长度为m和n的线条,计算两者的乘积 ...

  10. Android的selector 背景选择器

    关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法.首先android的selector是在d ...