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 再考虑四个塔 ...
随机推荐
- linux系统RabbitMQ启动错误记录
安装并配置好RabbitMq之后终端执行rabbitmq-server报错 试了网上的各种方法也无济于事 最后发现可能是因为访问权限的问题(并不确定) 解决方法:sudo rabbitmq-serve ...
- ML面试1000题系列(61-70)
本文总结ML面试常见的问题集 转载来源:https://blog.csdn.net/v_july_v/article/details/78121924 61.说说共轭梯度法? @wtq1993,htt ...
- PHP判断一个文件是否能够被打开
<?php // 需求:因为系统涉及大量的文档知识库,用户可以在线进行查看.为了验证文档是否正常打开.先需要从数据库取出路径和文件名,判断是否可以从对应的路径下打开文件.header(" ...
- linux小介绍
常用命令
- 构建PaaS的开源平台:CloudFoundry
CloudFoundry的架构: 点评:这是vmware用ruby开发的一个paas,由于对ruby不太熟悉,还是比较难理解. refer to :http://www.oschina.net/que ...
- FZU 1576【计算几何/费马点】
Oaiei居住在A城市,并且是这个城市建设的总设计师.最近有个问题一直困恼着他.A城市里有三个大型工厂,每个大型工厂每天都需要消耗大量的石油,现在城市里要建设一个石油中转站,从石油中转站到三个大型工厂 ...
- 第二周<导学/分类>
分类学习 分类算法各有不同 knn naivebyes regression dnn sklearn.linear_modlel 线性函数 sklearn.preprocessing 非线性函数 分类 ...
- [java]java构造器 标签: java面向对象 2017-06-11 11:16 195人阅读 评论(12)
构造器这个概念,各种语言都有出现,虽然为了考试或者其他学了好多遍,但是自己一直不能理解这个概念,前几天又学了一遍,突然就明白了,下面随小编来一起学习一下吧. 什么是构造器? 在类别基础的面向对象程序设 ...
- 2019.10.17beta
import socket import subprocess import os server = socket.socket() server.bind( ('127.0.0.1',8888) ) ...
- Python中if __name__ == '__main__':理解
在很多python脚本中在最后的部分会执行一个判断语句if __name__ == "__main__:",之后还可能会有一些执行语句.那添加这个判断的目的何在? 在python编 ...