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<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值