CF:392B - Tower of Hanoi 记忆化搜索DP
来源:互联网 发布:天津博学乐园网络书法 编辑:程序博客网 时间:2024/06/10 19:26
The Tower of Hanoi is a well-known mathematical puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
- Only one disk can be moved at a time.
- Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
- No disk may be placed on top of a smaller disk.
With three disks, the puzzle can be solved in seven moves. The minimum number of moves required to solve a Tower of Hanoi puzzle is2n - 1, where n is the number of disks. (c) Wikipedia.
SmallY's puzzle is very similar to the famous Tower of Hanoi. In the Tower of Hanoi puzzle you need to solve a puzzle in minimum number of moves, in SmallY's puzzle each move costs some money and you need to solve the same puzzle but for minimal cost. At the beginning of SmallY's puzzle all n disks are on the first rod. Moving a disk from rod i to rod j (1 ≤ i, j ≤ 3) costs tij units of money. The goal of the puzzle is to move all the disks to the third rod.
In the problem you are given matrix t and an integer n. You need to count the minimal cost of solving SmallY's puzzle, consisting of ndisks.
Each of the first three lines contains three integers — matrix t. The j-th integer in the i-th line is tij (1 ≤ tij ≤ 10000; i ≠ j). The following line contains a single integer n (1 ≤ n ≤ 40) — the number of disks.
It is guaranteed that for all i (1 ≤ i ≤ 3), tii = 0.
Print a single integer — the minimum cost of solving SmallY's puzzle.
0 1 11 0 11 1 03
7
0 2 21 0 1001 2 03
19
0 2 11 0 1001 2 05
87
题意:把n个盘子从第一个柱子放到第三个柱子的最少花费。
思路:
汉诺塔问题的移动方案其实就两种:
第一种:n-1个盘子从1通过3移动到2,最下面的从1移动到3,n-1个盘子从2通过1移动到3.
第二种:n-1个盘子从1通过2移动到3,最下面的从1移动到2,n-1个盘子从3通过2移动到1,最下面的从2移动到3,n-1个盘子从1通过2移动到3.
实际就是最下面盘子的两种移动方法..然后令Dp[n][i][j][k]表示n个盘子从i通过j移动到k的花费,记忆化搜索就行了。
#include <iostream>#include <cstdio>#include <fstream>#include <algorithm>#include <cmath>#include <deque>#include <vector>#include <list>#include <queue>#include <string>#include <cstring>#include <map>#include <stack>#include <set>#define PI acos(-1.0)#define mem(a,b) memset(a,b,sizeof(a))#define sca(a) scanf("%d",&a)#define pri(a) printf("%d\n",a)#define MM 10002#define MN 1005#define INF 168430090using namespace std;typedef long long ll;ll dp[50][4][4][4],a[3][4];int n,i,j;ll solve(int n,int a1,int a2,int a3){ if(!n) return 0; if(dp[n][a1][a2][a3]>=0) return dp[n][a1][a2][a3]; ll b1=solve(n-1,a1,a3,a2)+a[a1][a3]+solve(n-1,a2,a1,a3); ll b2=solve(n-1,a1,a2,a3)+a[a1][a2]+solve(n-1,a3,a2,a1)+a[a2][a3]+solve(n-1,a1,a2,a3); dp[n][a1][a2][a3]=min(b1,b2); return dp[n][a1][a2][a3];}int main(){ mem(dp,-1); for(i=1;i<=3;i++) for(j=1;j<=3;j++) cin>>a[i][j]; cin>>n; cout<<solve(n,1,2,3)<<endl; return 0;}
- CF:392B - Tower of Hanoi 记忆化搜索DP
- CF 392 B. Tower of Hanoi 记忆化搜索
- codeforces 392B Tower of Hanoi 记忆化搜索
- codefores 392B Tower of Hanoi (记忆搜索)
- Codeforces 392B Tower of Hanoi(递归+记忆化搜索)
- Codeforces 392B Tower of Hanoi(DP)
- Codeforces Round #230 (Div. 1) B. Tower of Hanoi 记忆化搜索
- Codeforce 392B Tower of Hanoi 区间dp
- codeforces 392 B. Tower of Hanoi(汉诺塔 DP)
- uva 10285 The Tower of Babylon(dp,记忆化搜索)
- uva 437 The Tower of Babylon(dp,记忆化搜索)
- Codeforces 392B Tower of Hanoi
- Tower of Hanoi CodeForces 392B
- CF 295div2 B dp(记忆化搜索)
- Codeforces_392B_Tower of Hanoi(记忆化搜索)
- 【进阶的汉诺塔】code forces 392B Tower of Hanoi
- POJ 1958 Strange Towers of Hanoi (四塔问题,线性dp,记忆化搜索)
- UVa 437.The Tower of Babylon【记忆化搜索+dp】【4月25】
- 下载图片
- NW(New World)快速开发平台介绍(完整的中小型管理系统解决方案)
- Android Sip学习(一)Android 2.3 APIs SIP-based VoIP
- JGibbLDA使用总结
- Thread.run()方法与Thread.start()方法的区别
- CF:392B - Tower of Hanoi 记忆化搜索DP
- 15章 模板
- (转)ucos的事件 任务的通讯和同步 信号量 互斥量 消息邮箱 消息队列
- javascript日期的表达
- Google的一道面试题
- 【D3.V3.js数据可视化系列教程】--(四)使用数据
- iOS开发常用的类别和宏定义集成frameworkd
- Tomcat启动与关闭事件监听
- YII中module下的视图如何自定义layout布局文件