POJ 3601 Tower of Hanoi
来源:互联网 发布:mysql scope identity 编辑:程序博客网 时间:2024/06/06 19:20
Description
The Tower of Hanoi is a puzzle consisting of three pegs and a number of disks of different sizes which can slide onto any peg. The puzzle starts with the disks neatly stacked in order of size on one peg, the smallest at the top, thus making a conical shape. The objective of the puzzle is to move the entire stack to another peg, obeying the following rules:
•Only one disk may be moved at a time.
•Each move consists of taking the upper disk from one of the pegs and sliding it onto another peg, on top of the other disks that may already be present on that peg.
•No disk may be placed on top of a smaller disk.
For n disks, it is a well-known result that the optimal solution takes 2n − 1 moves.
To complicate the puzzle a little, we allow multiple disks to be of the same size. Moreover, equisized disks are mutually distinguishable. Their ordering at the beginning should be preserved at the end, though it may be disturbed during the process of solving the puzzle.
Given the number of disks of each size, compute the number of moves that the optimal solution takes.
【题目分析】
汉诺塔问题的变种,同样的汉诺塔会有多个,也没有什么不同,只需要分类dp一下就可以了。
【代码】
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int dp1[105],dp2[105], num[105];int main(){ int n,m; while(cin>>n>>m) { for(int i=1;i<=n;i++) cin>>num[i]; dp1[1]=num[1]; for(int i=2;i<=n;i++) dp1[i]=(2*dp1[i-1]+num[i])%m; dp2[1]=2*(num[1]-1)+1; for(int i=2;i<=n;i++) { if(num[i]==1) dp2[i]=dp1[i]%m; else dp2[i]=(2*dp1[i-1]+2*num[i]+dp2[i-1])%m; } cout<<dp2[n]<<endl; } return 0;}
- poj 3601 Tower of Hanoi
- POJ 3601:Tower of Hanoi
- POJ 3601 Tower of Hanoi
- [poj 3601]Tower of Hanoi 数论
- POj 3601 Tower of Hanoi 汉诺塔(贪心)
- pku 3601 Tower of Hanoi
- THE TOWER OF HANOI
- Tower of Hanoi
- Tower of Hanoi
- Tower of Hanoi
- Tower of Hanoi问题
- codeforces Tower of Hanoi
- Tower of Hanoi
- 汉诺塔 Tower of Hanoi
- THE TOWER OF HANOI
- 汉诺塔(the Tower of Hanoi )
- 1.1 The Tower of Hanoi
- Tower of Hanoi by C++
- Codevs 1035 火车停留 [费用流] [拆点]
- Android view背景动态改变带简单淡入淡出动画
- poj3260&&hdu3591 多重背包+完全背包
- 1023. Have Fun with Numbers (20)
- 动态库的编写和使用
- POJ 3601 Tower of Hanoi
- Effecitive C++_Item20笔记
- 多线程——继承Thread 类和实现Runnable 接口的区别
- 公告
- JAVA ASCII码 读入字符串测试
- C与C++中的enum类型使用
- 在Ubuntu中安装Visual Studio Code
- Begin_With_AngularJS(1)基础:ng-app,ng-controller,代码分离,模板,作用域等
- 欧拉函数模板