poj 3572 Hanoi Tower
2016-01-22 15:53
274 查看
Hanoi Towers
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)Total Submission(s) : 0 Accepted Submission(s) : 0
Special Judge
[align=left]Problem Description[/align]
The “Hanoi Towers” puzzle consists of three pegs (that we will name A, B, and C) with n disks of different diameters stacked onto the pegs. Initially all disks are stacked onto peg A with the smallest disk at the top and the largest one at the bottom, so that they form a conical shape on peg A.
[align=left]Input[/align]
The input file contains two lines. The first line consists of a single
integer number n (1 ≤ n ≤ 30) the number of disks in the puzzle.
The second line contains descriptions of six moves separated by spaces the
strategy that is used to solve the puzzle.
[align=left]Output[/align]
Write to the output file the number of moves it takes to solve the puzzle.
This number will not exceed 1018.
[align=left]Sample Input[/align]
#1 | 3 AB BC CA BA CB AC |
---|---|
#2 | 2 AB BA CA BC CB AC |
#1 | 7 |
---|---|
#2 | 5 |
题意:对于一个汉诺塔,每次按照给定的顺序,找第一个可以移动的方式去移动。要求不能大的下面放小的,且不能连续两次移动同一个盘子。
附上代码:
#include<cstdio> #include<cstring> using namespace std; typedef long long ll; ll a[40],b[40],c[40]; int num[10]; char s[10]; int main() { int n,i,j,k; a[1]=b[1]=c[1]=1; for(i=2; i<=30; i++) { a[i]=a[i-1]*2+1; c[i]=c[i-1]*3+2; b[i]=b[i-1]+c[i-1]+1; } scanf("%d",&n); for(i=1; i<=6; i++) { scanf("%s",s); k=(s[0]-'A')*4+s[1]-'A'; num[k]=i; } if(num[1]<num[2]) { if(num[4]<num[6]) printf("%I64d\n",c ); else if(num[9]<num[8]) printf("%I64d\n",b ); else printf("%I64d\n",a ); } else { if(num[8]<num[9]) printf("%I64d\n",c ); else if(num[6]<num[4]) printf("%I64d\n",b ); else printf("%I64d\n",a ); } }
相关文章推荐
- Cloud Insight 客户案例-晨芯时代科技有限公司
- TBluetoothLEDevice.UpdateOnReconnect
- html/css基础篇——link和@inport详解以及脚本执行顺序探讨
- Python里小程序
- ubuntu中desktop与alternate版本的区别
- CPU 资源消耗原因和解决方案
- 在js中'与"的区别
- android input子系统之一:驱动部分
- goroutine
- c++多重继承
- R语言の快速排序
- jquery定位div中的滚动条
- nginx的location匹配逻辑(翻译自官网)
- Linux内核之/proc节点
- 坚持真的是一件很难的事情
- ThreadPoolExecutor机制
- 详解C++编程中的静态成员与可变数据成员
- POJ 2709 Painter
- OBJECTIVE-C中nsstring的writetofile atomically参数解释
- Android 6.0 Marshmallow 的改变