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 再考虑四个塔 ...
随机推荐
- 移动端h5禁用浏览器左滑右滑的前进后退功能
在项目运行过程中发现,用户在有左右滑动前进后退的功能的浏览器上签字时,偶然触发了前进后退会导致canvas像是重置了一样内容消失,所以需要在代码中处理这种情况. 基本原理就是在touchmove事件中 ...
- Chrome浏览器一直请求clients1.google.com:443
浏览器莫名其妙地发一大堆请求,往clients1.google.com:443,把各种扩展各种插件关了都不管用,后来才发现问题,取消“密码和表单”中的“自动填充”功能,即可解决.
- Leetcode5.Longest Palindromic Substring最长回文字串
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &quo ...
- 【风马一族_SQL Server】
原文来自:http://www.cnblogs.com/sows/p/6097684.html (博客园的)风马一族 侵犯版本,后果自负 2016-11-24 14:25:45 命令行方式处理服务管 ...
- Linux之rpm包管理-yum在线管理
1.IP地址配置 1.以root登录Linux系统,在终端输入setup启动图形界面menuconfing,如下图所示: 2.选择network configuration ,进入网络配置界面,进入后 ...
- JS黑科技
1.论如何优雅的取随机字符串 Math.random().toString(16).substring(2) // 13位 Math.random().toString(36).substring(2 ...
- 【祈福】NOIP战后占卜:众星陨落,天命难违
Day1 加上看题,做完第一题之后我已经只剩两个小时半了. 然后凭着一定要做完第一题和第二题的坚定信念. 我耗到了只剩一个小时半,结果正解还是没想出来. 其实我从只剩两小时的时候,就有了打第二题的暴力 ...
- springboot(十九)使用actuator监控应用【转】【补】
springboot(十九)使用actuator监控应用 微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的 ...
- Directx11教程41 纹理映射(11)
原文:Directx11教程41 纹理映射(11) 1.第一副图我们采用各性异性的滤波方式,并设置最大各性异性值为8. samplerDesc.Filter = D3D11_FILT ...
- 配置一个Oracle共享服务器进程环境需要哪两项参数
SHARED_SERVERS和DISPATCHERS. PROTOCOL(pro或prot): 调度程序要监听的网络协议.这是唯一必需的属性 ADDRESS(ADD或者ADDR): 指定调度程序正在上 ...