Tower of Hanoi CodeForces 392B

B. Tower of Hanoi
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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:
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.
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.
Examples
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

题意
本题就是汉诺塔问题的变形。每个盘子挪动的花费如题干中给出的矩阵所示,求最少的花费。
其中要运用floyd算法求一下只有一个盘子的时候的花费。
然后对于每种情况再进行dfs处理。
采用递归算法+记忆化搜索的思路。
与之前的汉诺塔比较即可发现,此题目的状态除了盘子的个数,还和从哪个盘子到哪个有关。
需要注意的是 ,会爆int,要开long long来存答案。


#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
ll dp[5][5][50];
ll flo[10][10];
int cost[10][10];
int n;


bool vis[5][5][50];


ll dfs(int a,int b,int all)
{  if(all==1)
        return flo[a][b];
    if(vis[a][b][all])
        return dp[a][b][all];
    vis[a][b][all]=1;


    int c=3-(a+b);
    ll t1=dfs(a,c,all-1)+cost[a][b]+dfs(c,b,all-1);
    ll t2=dfs(a,b,all-1)+cost[a][c]+dfs(b,a,all-1)+cost[c][b]+dfs(a,b,all-1);
    return dp[a][b][all]=min(t1,t2);
}


void solve()
{
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
            {
                scanf("%d",&cost[i][j]);
                flo[i][j]=cost[i][j];
            }
    }
    memset(dp,0,sizeof dp);
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            if(i==j)
                continue;
            for(int k=0;k<3;k++)
            {
                if(i!=j&&i!=k&&k!=j)
                flo[i][j]=min(flo[i][j],flo[i][k]+flo[k][j]);
            }
        }
    }
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
            dp[i][j][1]=flo[i][j];
    }


    scanf("%d",&n);
    printf("%lld\n",dfs(0,2,n));
}
int main()
{
    solve();


    return 0;
}

你可能感兴趣的:(模拟,搜索)