poj 1920 Towers of Hanoi
Time Limit: 3000MS | Memory Limit: 16000K | |
Total Submissions: 2213 | Accepted: 986 | |
Case Time Limit: 1000MS |
Description
According to an old myth, the monks at an ancient Tibetian monastery have been trying to solve an especially large instance of this problem with 47 disks for thousands of years. Since this requires at least 247 - 1 moves and the monks started out without a strategy, they messed it all up while still following the rules. Now they would like to have the disks stacked up neatly on any arbitrary peg using the minimum number of moves. But they all took a vow which forbids them to move the disks contrary to the rules. They want to know on which peg they should best stack the disks, and the minimum number of moves needed.
Write a program that solves this problem for the monks. Your program should also be able to handle any number N (0 < N <= 100 000) of disks. The numbers involved in the computation can become quite large. Because of that, the monks are only interested in the number of moves modulo 1 000 000.
Example
The following example can be solved in four moves.
Input
The (i + 2)-th line of the input file consists of integer numbers mi,1 . . .mi,si with 1 <= mi,j <= N, the sizes of the disks on peg i. The disks are given from bottom to top, thus mi,1 > mi,2 > . . . > mi,si .
Note that an empty stack is given by an empty line. The set of N disks have different sizes. All numbers are separated by a single space.
Output
Sample Input
7
2 1 4
2 1
3
7 6 5 4
Sample Output
3
4
Source
第一行输出一个数字表示集中到哪个柱子上,第二行输出一个数字表示最小步数模1000000
附上代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int T,i,j,n,m;
int a[],b[],c[];
while(~scanf("%d",&T))
{
for(i=; i<=; i++)
scanf("%d",&a[i]);
for(i=; i<=; i++)
{
for(j=; j<=a[i]; j++)
{
scanf("%d",&n);
b[n]=i; //记录每个盘子所在的柱子位置
}
}
c[]=;
for(i=; i<T; i++)
c[i+]=(c[i]*)%;
int s1=b[T],s2=b[T-],s=; //s1为最大的盘子位置,s2为第二大的盘子位置
for(i=T-; i>; i--,s2=b[i])
{
if(s1!=s2) //假如盘子不在正确的位置上,将其移动
{
s=(s+c[i-])%;
s1=-s1-s2; //记录剩余盘子新的位置
}
}
printf("%d\n%d\n",b[T],s);
}
return ;
}
poj 1920 Towers of Hanoi的更多相关文章
- 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 ...
- 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 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 题意:汉诺 ...
- POJ1958 Strange Towers of Hanoi [递推]
题目传送门 Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3117 Ac ...
- 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 ...
- 【POJ 1958】 Strange Towers of Hanoi
[题目链接] http://poj.org/problem?id=1958 [算法] 先考虑三个塔的情况,g[i]表示在三塔情况下的移动步数,则g[i] = g[i-1] * 2 + 1 再考虑四个塔 ...
随机推荐
- 【CS Round #44 (Div. 2 only) B】Square Cover
[链接]点击打开链接 [题意] 给你一个n*m的矩形,让你在其中圈出若干个子正方形,使得这些子正方形里面的所有数字都是一样的. 且一样的数字,都是在同一个正方形里面.问你有没有方案. [题解] 相同的 ...
- SQLServer2005 没有日志文件(*.ldf) 只有数据文件(*.mdf) 恢复数据库的方法
代码如下: exec sp_attach_db exun,'d:\exun2.mdf' (可能执行一次不能成功,测试了下,有时候需要执行2次以上命令才行) 执行了之后,记得刷新数据库,不然是不会显示的
- using namespace std 和 include 的区别
using namespace std; int main() { std::string dd = "ddd";//会报错namespace "std" h ...
- Floyd算法模板--详解
对于无权的图来说: 若从一顶点到另一顶点存在着一条路径,则称该路径长度为该路径上所经过的边的数目,它等于该路径上的顶点数减1. 由于从一顶点到另一顶点可能存在着多条路径,每条路径上所经过的边数可能不同 ...
- java 3类的继承
模板类 泛型程序设计方法 类的组合 类的继承 java只有单继承 隐藏和覆盖 用super.x调用 访问静态属性 静态属性不继承 静态成员只有一个,不会有副本 静态成员只有一个所有的超类和子类 方法的 ...
- 云服务器 ECS Linux Web 环境配置站点的方法
摘自:https://help.aliyun.com/knowledge_detail/41100.html ECS Linux 系统一键安装 Web 环境<专业版>下 Tomcat 添加 ...
- C++ 浮点数 为 0 的判断
- R语言与非参数统计(核密度估计)
R语言与非参数统计(核密度估计) 核密度估计是在概率论中用来估计未知的密度函数,属于非参数检验方法之一,由Rosenblatt (1955)和Emanuel Parzen(1962)提出,又名Parz ...
- idea 项目热部署设置
1.引入pom.xml() <!-- 热部署(必须) --> <dependency> <groupId>org.springframework.boot</ ...
- 【时光回溯】【JZOJ3568】【GDKOI2014】小纪的作业题
题目描述 输入 输出 有M行,每个询问一行,输出结果mod 1,000,000,007的值. 样例输入 10 3 3 5 1 2 3 1 3 5 2 1 7 9 3 9 2 3 样例输出 10 19 ...