Codeforces 392B Tower of Hanoi

本文探讨了一个类似于著名的Hanoi塔的谜题,其中每次移动的成本不同,目标是在最小成本下解决该谜题。通过给定的矩阵表示不同路径的成本,以及盘子的数量,读者将了解到如何计算最小成本解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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
 以前很少做记忆化的题目,这次这个题要记忆化很简单,但是找递归的式子,足足让我找了一天。。。一开始把问题想麻烦了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
__int64 a[4][4];
__int64 dp[50][4][4];
int main()
{
    //freopen("data.txt","r",stdin);
    __int64 f(int n,int s,int e,int tr);
    for(int i=1;i<=3;i++)
    {
        for(int j=1;j<=3;j++)
        {
            scanf("%I64d",&a[i][j]);
        }
    }
    int n;
    scanf("%d",&n);
    memset(dp,-1,sizeof(dp));
    __int64 k=f(n,1,3,2);
    printf("%I64d\n",k);
    return 0;
}
__int64 f(int n,int s,int e,int tr)
{
    __int64 res=0;
    if(dp[n][s][e]!=-1)
    {
        return dp[n][s][e];
    }
    if(n==1)
    {
        __int64 k = min(a[s][e],a[s][tr]+a[tr][e]);
        res+=k;
    }else
    {
       __int64 val1 = f(n-1,s,tr,e)+a[s][e];
       val1+=f(n-1,tr,e,s);
       __int64 val2 = f(n-1,s,e,tr)+a[s][tr];
       val2+=f(n-1,e,s,tr)+a[tr][e];
       val2+=f(n-1,s,e,tr);
       res+=min(val1,val2);
    }
    dp[n][s][e] = res;
    return res;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值