Towers of Hanoi
Time Limit: 3000MS   Memory Limit: 16000K
Total Submissions: 2213   Accepted: 986
Case Time Limit: 1000MS

Description

Surely you have already come across the Towers of Hanoi problem: Wooden disks of different sizes are stacked on three pegs, and initially, all disks are stacked on the same peg sorted by size, with the largest disk at the bottom. The objective is to transfer the entire tower to one of the other pegs, moving only one disk at a time and never putting a larger disk onto a smaller one. 
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 first line of the input file hanoi.in consists of the number N (N <= 100000) of disks. The second line consists of three integers s1, s2, s3 with 0 <= s1, s2, s3 <= N and s1+s2+s3 = N, the number of disks on each of the three pegs. Lines three to five each contain the sizes of the disks for one peg. More precisely: 
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

The first line of the output file hanoi.out consists of the number d in {1, 2, 3} of the peg onto which the disks can be stacked using the minimum number of moves. The second line consists of the number M of required moves modulo 1 000 000.

Sample Input

7
2 1 4
2 1
3
7 6 5 4

Sample Output

3
4

Source

 
用了汉诺塔的非递归算法,如果有n块要全部移动到C上的话,就需移动2^n-1,这是最基本的汉诺塔求解问题。再来看这一题,他要求我们把其中一个状态移动到一座塔上,其实也就是把一座塔移动到当前状态就好了,去画一下就知道又变成了基本的汉诺塔问题了。注意:最大的那块是不动的,从公式可以看出。
 
题意:给定一个汉诺塔的局面,问最少多少步可以将它们并到一个柱子上。
第一行n,表示n个盘子接下来一行3个数字m[i],表示柱子上分别有几个盘子随后的三行每行m[i]个数字,表示在该柱子上的盘子的编号

第一行输出一个数字表示集中到哪个柱子上,第二行输出一个数字表示最小步数模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的更多相关文章

  1. 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 ...

  2. POJ 1958 Strange Towers of Hanoi

    Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3784 Accepted: 23 ...

  3. The Towers of Hanoi Revisited---(多柱汉诺塔)

    Description You all must know the puzzle named "The Towers of Hanoi". The puzzle has three ...

  4. [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 ...

  5. POJ-1958 Strange Towers of Hanoi(线性动规)

    Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 2677 Accepted: 17 ...

  6. ural 2029 Towers of Hanoi Strike Back (数学找规律)

    ural 2029 Towers of Hanoi Strike Back 链接:http://acm.timus.ru/problem.aspx?space=1&num=2029 题意:汉诺 ...

  7. POJ1958 Strange Towers of Hanoi [递推]

    题目传送门 Strange Towers of Hanoi Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3117   Ac ...

  8. 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 ...

  9. 【POJ 1958】 Strange Towers of Hanoi

    [题目链接] http://poj.org/problem?id=1958 [算法] 先考虑三个塔的情况,g[i]表示在三塔情况下的移动步数,则g[i] = g[i-1] * 2 + 1 再考虑四个塔 ...

随机推荐

  1. 创建多个Django业务模块

    manage.py startapp cmdb 如果运行错误记得加环境变量 c:\python35\python.exe manage.py startapp cmdb

  2. Vue-- vue-preview(图片查看器)的使用步骤:

    vue-preview的使用步骤: )下载 )配置:找到配置文件加入: { test: /vue-preview.src.*?js$/, loader: 'babel-loader' }, 在处理ur ...

  3. 三、Redis操作

    一.基本介绍 1.简介 Redis是由意大利人Salvatore Sanfilippo(网名:antirez)开发的一款内存高速缓存数据库.Redis全称为:Remote Dictionary Ser ...

  4. Spring Boot:Boot2.0版本整合Neo4j

    前面介绍了Boot 1.5版本集成Neo4j,Boot 2.0以上版本Neo4j变化较大. 场景还是电影人员关系 Boot 2.0主要变化 GraphRepository在Boot2.0下不支持了,调 ...

  5. python ndarray相关操作:转置、翻转

  6. let 和const命令

    ES6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效.换句话说,let声明了块级作用域. 输出 看下面代码: 输出: 变量i是var声明的,在 ...

  7. word Stock Market Indices

    Stock Market Indices USA Africa Asia and Pacific Canada Europe Middle East South America Internation ...

  8. Hibernate的DetachedCriteria使用(含Criteria)转载

    https://www.cnblogs.com/deng-cc/p/6428599.html 1.背景了解:Hibernate的三种查询方式 Hibernate总的来说共有三种查询方式:HQL.QBC ...

  9. day3-转自金角大王

    本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 温故知新 1. 集合 主要作用: 去重 关系测 ...

  10. Directx教程(23) 简单的光照模型(2)

    原文:Directx教程(23) 简单的光照模型(2)    在工程myTutorialD3D11_16中,我在文件light.vs中定义了一个材质光源属性常量缓冲. //const buffer最好 ...