SGU 202 The Towers of Hanoi Revisited (DP+递归)
转载请注明出处,谢谢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+递归)的更多相关文章
- SGU 202. The Towers of Hanoi Revisited
多柱汉诺塔问题. 引用自wiki百科 多塔汉诺塔问题 在有3个柱子时,所需步数的公式较简单,但对于4个以上柱子的汉诺塔尚未得到通用公式,但有一递归公式(未得到证明,但目前为止没有找到反例): 令为在有 ...
- 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] ...
- 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 ...
- 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 ...
- ural 2029 Towers of Hanoi Strike Back (数学找规律)
ural 2029 Towers of Hanoi Strike Back 链接:http://acm.timus.ru/problem.aspx?space=1&num=2029 题意:汉诺 ...
随机推荐
- 一、富有表现力的JavaScript
第一章:富有表现力的JavaScript 1.1 JavaScript的灵活性 1.2 弱类型语言 1.3 函数是一等对象 1.4 对象的易变性 1.5 继承 1.6 JavaScript ...
- jQuery给table中的负数标红色
<table class="tb_list"></table> $(function(){ $(".tb_list td").each( ...
- TextView settextcolor 无效解决方案
viHolder.order_item_tipcolor.setBackgroundColor(context .getResources().getColor(R.color.order_xixie ...
- 初来乍到--------smarty
Smarty Day01-----smarty的使用 作用:把php+html分离,程序和美工 使用: 1.下载源码包 2.目录结构 libs 源码文件 Smarty.class.php templa ...
- 在ProgressBar上加文字----显示百分比的进度条
http://www.cnblogs.com/3dant/archive/2011/04/25/2026776.html
- 为什么Java项目前会出现一个红色感叹号!
先看看问题,如下图所示: 造成这个问题的原因是,我把一个 jar 包删除了,然后又配了个新的进去,然后就一直有这个错误,刚开始很郁闷,怎么已经配置过儿,还出现这个问题?关键是代码里面没有报错的.郁闷的 ...
- 安装64位Oracle 10g超详细教程
安装64位Oracle 10g超详细教程 1. 安装准备阶段 1.1 安装Oracle环境 经过上一篇博文的过程,已经完成了对Linux系统的安装,本例使用X-Manager来实现与Linux系统的连 ...
- ZOJ 1698 (最大流入门)
Power NetworkTime Limit:5000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu SubmitStat ...
- 【课程分享】ASP.NET MVC5&微信公众平台整合开发实战(响应式布局、JQuery Mobile,Windows Azure、微信核心开发)
对这个课程有兴趣的,能够联系我QQ2748165793 基础知识储备 ASP.NET MVC 5基础(6讲) 第一讲-初识ASP.NET MVC并搭建整合开发环境 第二讲-深入MVC开发模式 第三讲- ...
- hdu 4612 Warm up 双连通缩点+树的直径
首先双连通缩点建立新图(顺带求原图的总的桥数,事实上因为原图是一个强连通图,所以桥就等于缩点后的边) 此时得到的图类似树结构,对于新图求一次直径,也就是最长链. 我们新建的边就一定是连接这条最长链的首 ...