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 再考虑四个塔 ...
随机推荐
- excel之VLOOKUP函数的使用
VLOOKUP 函数是excel中比较常用的一个函数.该函数具有有四个参数: 1.查找值:指本表中的值,需要根据本表中的某一值在本表或其他表中查找我们想要获取的值就称为查找值. 2.数据表:指查找的范 ...
- SVN 打包时,出现File not found: transaction '148-48', path ''https://xxxxxx/svn/xxxxx/tag/2017-9-30'
svn打包时,出现错误如下: 解决办法: 在SVN目录结构中,增加tag这个目录.
- onethink上传到服务器(或者迁移)后台登录验证码错误问题
修改Application\User下面的配置文件config.php, 改成对应服务器上的参数 define('UC_DB_DSN', 'mysqli://root:root@127.0.0.1:3 ...
- Markdown Linux
如何在Linux下使用Markdown进行文档工作 学习于: http://www.ituring.com.cn/article/10044 Markdown 官网: http://daringfir ...
- nodeJs学习-17 博客案例
源码:智能社视频20节课件 const express=require('express'); const static=require('express-static'); const cookie ...
- 10种简单的Java性能优化(转)
本文由 ImportNew - 一直在路上 翻译自 jaxenter.欢迎加入翻译小组.转载请见文末要求. 你是否正打算优化hashCode()方法?是否想要绕开正则表达式?Lukas Eder介绍了 ...
- vue-cnodejs
感谢那些无私开源的程序员,你们是最可爱的人儿~~~~ //根app app.js <template> <div id="app"> <v-heade ...
- Inno Setup生成桌面快捷方式
在做项目的时候,需要打包成exe安装包.先前使用的是vs来打包,生成了setup.exe 和 *.msi的安装文件,不过也算顺利. 后因为要求采取 Inno Setup来打包程序,其中遇到个创建快捷方 ...
- php 获取客户端的ip、地理信息、浏览器信息、本地真实ip
转自:http://www.blhere.com/948.html 这是非常实用的php常用类.获取客户端的ip.地理信息.浏览器信息.本地真实ip 1234567891011121314151617 ...
- 猜年龄v2.0
''' 用户登录,只有三次机会 给定年龄,用户可以猜三次年龄 年龄猜对,让用户选择两次奖励,输入无效字符,让其选择要不要礼物 用户选择两次奖励后可以退出,选择第一次后提示还有一次 ''' #基本信息定 ...