博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 1920 Towers of Hanoi
阅读量:4953 次
发布时间:2019-06-12

本文共 3663 字,大约阅读时间需要 12 分钟。

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 2
47 - 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 m
i,1 . . .m
i,si with 1 <= m
i,j <= N, the sizes of the disks on peg i. The disks are given from bottom to top, thus m
i,1 > m
i,2 > . . . > m
i,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

72 1 42 137 6 5 4

Sample Output

34

Source

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

第一行输出一个数字表示集中到哪个柱子上,第二行输出一个数字表示最小步数模1000000

 

附上代码:

1 #include 
2 #include
3 #include
4 using namespace std; 5 int main() 6 { 7 int T,i,j,n,m; 8 int a[5],b[100005],c[100005]; 9 while(~scanf("%d",&T))10 {11 for(i=1; i<=3; i++)12 scanf("%d",&a[i]);13 for(i=1; i<=3; i++)14 {15 for(j=1; j<=a[i]; j++)16 {17 scanf("%d",&n);18 b[n]=i; //记录每个盘子所在的柱子位置19 }20 }21 c[0]=1;22 for(i=0; i
0; i--,s2=b[i])26 {27 if(s1!=s2) //假如盘子不在正确的位置上,将其移动28 {29 s=(s+c[i-1])%1000000;30 s1=6-s1-s2; //记录剩余盘子新的位置31 }32 }33 printf("%d\n%d\n",b[T],s);34 }35 return 0;36 }

 

转载于:https://www.cnblogs.com/pshw/p/5151365.html

你可能感兴趣的文章
8.1 Android Basic 数据存储 Preferences Structured(分组的Preferences)
查看>>
原因和证明
查看>>
VC6.0图像处理2--图像的反色
查看>>
Snoop, 对WPF程序有效的SPY++机制
查看>>
JAVA程序猿怎么才干高速查找到学习资料?
查看>>
使用axel下载百度云文件
查看>>
Qt中图像的显示与基本操作
查看>>
详解软件工程之软件测试
查看>>
WCF(二) 使用配置文件实现WCF应用程序
查看>>
【CodeForces 803 C】Maximal GCD(GCD+思维)
查看>>
python 去掉换行符或者改为其他方式结尾的方法(end='')
查看>>
数据模型(LP32 ILP32 LP64 LLP64 ILP64 )
查看>>
REST构架风格介绍:状态表述转移
查看>>
c++ operator
查看>>
java小技巧
查看>>
POJ 3204 Ikki's Story I - Road Reconstruction
查看>>
网页消息类
查看>>
【BZOJ】2959: 长跑(lct+缩点)(暂时弃坑)
查看>>
日常一些出现bug的问题
查看>>
同时启动多个tomcat服务器
查看>>