Codeforces_392B_Tower of Hanoi(记忆化搜索)

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 is 2n - 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 n disks.

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 1
1 0 1
1 1 0
3
output
7
input
0 2 2
1 0 100
1 2 0
3
output
19
input
0 2 1
1 0 100
1 2 0
5
output
87

题型:动态规划


题意:

        汉诺塔游戏,给出矩阵a[i][j],表示将一个盘子从i柱子移到j柱子要花费a[i][j],再给出n,问将这n个盘子从1柱子移到3柱子的最少花费。


分析:

原来初学递归的时候,就感受到了用递归解决汉诺塔问题的奇妙~

仔细分析,可以发现,汉诺塔的移到不外乎俩情况:

1、上面n-1个盘子从1通过3移到了2,

      最下面的盘子从1移到了3,

      上面n-1个盘子从2通过1移到了3;

2、上面n-1个盘子从1通过2移到了3,

      最下面的盘子从1移到了2,

      上面n-1个盘子从3通过2移到了1;

      最下面的盘子从2移到了3,

      上面n-1个盘子从1通过2移到了3;

定义数组dp[n][from][by][to]表示上面n个盘子从from 通过by移到了to所花费的最少的cost,然后递归下去进行记忆化搜索。


代码:

#include
#include
#include
#include
#define LL long long
using namespace std;

LL a[4][4],dp[41][4][4][4];

LL get(int n,int p1,int p2,int p3){
    if(n==1) return min(a[p1][p3], a[p1][p2] + a[p2][p3]);
    if(dp[n][p1][p2][p3]!=-1) return dp[n][p1][p2][p3];
    LL tmp1=get(n-1,p1,p3,p2)+a[p1][p3]+get(n-1,p2,p1,p3);
    LL tmp2=get(n-1,p1,p2,p3)+a[p1][p2]+get(n-1,p3,p2,p1)+a[p2][p3]+get(n-1,p1,p2,p3);
    dp[n][p1][p2][p3] = min(tmp1,tmp2);
    return dp[n][p1][p2][p3];
}

int main(){
    int n;
    memset(dp,-1,sizeof(dp));
    for(int i=1;i<=3;i++){
        for(int j=1;j<=3;j++){
            scanf("%d",&a[i][j]);
        }
    }
    scanf("%d",&n);
    printf("%I64d\n",get(n,1,2,3));
    return 0;
}


你可能感兴趣的:(ACM)