Codeforces Round #230 (Div. 1) B. Tower of Hanoi 记忆化搜索

来源:互联网 发布:模拟学生选课代码java 编辑:程序博客网 时间:2024/05/22 05:09

B. Tower of Hanoi
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  1. Only one disk can be moved at a time.
  2. 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.
  3. 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.

Input

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.

Output

Print a single integer — the minimum cost of solving SmallY's puzzle.

Sample test(s)
input
0 1 11 0 11 1 03
output
7
input
0 2 21 0 1001 2 03
output
19
input
0 2 11 0 1001 2 05
output
87

题目大意:

汉诺塔背景,同样是三个柱子,给出三个柱子之间的移动花费矩阵。

要求将一开始在第一个柱子上的n个碟子移动到第三个柱子上的最小花费。


解题思路&反省:

这道题完完全全是自己想出来,有点开心,算是正式迈入acmer的门槛了。


先来看看网上其他人的做法,

1. dp[x][a][c]=dp[x-1][a][b]+cost[a][c]+dfs[x-1][b][c];

2. dp[x][a][c]=dp[x-1][a][c]+cost[a][b]+dp[x-1][c][a]+cost[b][c]+dp[x-1][a][c];

基本上都是这两个转移方程,但是在自己的思考过程中,我注意到,第二个公式的最后一项  dp[x-1][a][c]  还可以是 dp[x-1][a][b]+dp[x-1][b][c]

我和网上其他人的细微差别就在这里,因为自己想了一下似乎不能证明这样的第三个式子一定劣于以上两个,但是实际情况是的,以后思考可以再细致一点

所以我的代码里面会有一个split函数,就是用于分解每一步的,因为每一个移动其实都有两种方式,一个是直接移动到目的地,另一种是先经过中介再移动到目的地

split函数就包含了这两张情况的比较


另外,还有一些解题过程中出现的问题

(1)事先没有考虑数据范围,爆了int后WA了才发现

(2)我原先的算法不够严谨,当n==1的时候,dfs函数并没有比较直接移动和间接移动哪个花费更少


下面是ac代码:

#include <iostream>#include <string.h>#include <stdio.h>#include <stdlib.h>#include <math.h>#include <memory.h>#include <string>#include <vector>#include <list>#include <map>#include <queue>#include <stack>#include <bitset>#include <algorithm>#include <numeric>#include <functional>#define maxn 45using namespace std;typedef int64_t ll;ll n;ll dp[maxn][3][3];ll cost[3][3];ll split(ll n,ll from,ll to);ll dfs(ll n,ll from,ll to);ll split(ll n,ll from,ll to){    if(!n) return 0;    ll mid;    for(ll i=0;i<3;i+=1)        if(i!=from&&i!=to)            mid=i;    return min(dfs(n,from,to),dfs(n,from,mid)+dfs(n,mid,to));}ll dfs(ll n,ll from,ll to){    /*if(n==1) return dp[n][from][to]=cost[from][to];    //当只剩下一块的时候,说明他是最底层的最大的,所以这里一定不能够比较直接移动和间接移动    //因为会出现压到其他比他小的盘子上,而这里不比较的话,当最初n就为1的时候又会不符合情况    /*else */if(dp[n][from][to]!=-1) return dp[n][from][to];    ll mid;    for(ll i=0;i<3;i+=1)        if(i!=from&&i!=to)            mid=i;    dp[n][from][to]=min(split(n-1,from,mid)+cost[from][to]/*本来是dfs(1,from,to)*/+split(n-1,mid,to)    ,    split(n-1,from,to)+cost[from][mid]/*本来是dfs(1,from,mid)*/+split(n-1,to,from)+cost[mid][to]/*本来是dfs(1,mid,to)*/+split(n-1,from,to));    return dp[n][from][to];}int main(){    memset(dp,-1,sizeof(dp));    for(int i=0;i<3;i+=1){        for(int j=0;j<3;j+=1){            cin>>cost[i][j];        }    }    cin>>n;    /*if(n==1) cout<<min(cost[0][2],cost[0][1]+cost[1][2]);    else */cout<<dfs(n,0,2);    return 0;}/*0 1 11 0 11 1 030 1 101 0 110 1 01*/


这是另一个ac版本,使用dp递推循环写的,时间是上面的两倍,上面是31ms,这个是62ms:

#include <iostream>#include <string.h>#include <stdio.h>#include <stdlib.h>#include <math.h>#include <memory.h>#include <string>#include <vector>#include <list>#include <map>#include <queue>#include <stack>#include <bitset>#include <algorithm>#include <numeric>#include <functional>#define maxn 45using namespace std;typedef int64_t ll;ll n;ll dp[maxn][3][3];ll cost[3][3];int main(){    for(int i=0;i<n+1;i+=1){        for(int j=0;j<3;j+=1){            for(int k=0;k<3;k+=1){                dp[i][j][k]=0;            }        }    }    for(int i=0;i<3;i+=1){        for(int j=0;j<3;j+=1){            cin>>cost[i][j];        }    }    cin>>n;    if(n==1){        cout<<min(cost[0][2],cost[0][1]+cost[1][2]);        return 0;    }    else        for(int i=1;i<=n;i+=1){            for(int a=0;a<3;a+=1){                for(int c=0;c<3;c+=1){                    if(a!=c){                        int b=3-a-c;                        dp[i][a][c]=min(dp[i-1][a][b]+cost[a][c]+dp[i-1][b][c],dp[i-1][a][c]+cost[a][b]+dp[i-1][c][a]+cost[b][c]+dp[i-1][a][c]);                        //注意,一种错误的写法是在这里讨论n==1的情况,也就是加一个if然后在下面写一句                        //dp[i][a][c]=min(cost[0][2],cost[0][1]+cost[1][2]);                        //但是,dp[1][a][c]是要为之后所用的,也就是作为最下面最大的盘子,而这个时候是不能间接移动操作的                    }                }            }        }    cout<<dp[n][0][2];    return 0;}















0 0
原创粉丝点击