codeforces 392 B. Tower of Hanoi(汉诺塔 DP)
来源:互联网 发布:拍照软件mix 编辑:程序博客网 时间:2024/05/22 10:44
题目链接:http://codeforces.com/contest/392/problem/B
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.
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.
Print a single integer — the minimum cost of solving SmallY's puzzle.
0 1 11 0 11 1 03
7
0 2 21 0 1001 2 03
19
0 2 11 0 1001 2 05
87
题意:(转)
在原来的汉诺塔基础上,给每次移动加上一个权值,Tij表示从rodi移动到rodj的花费。
求把n个盘子从rod1移动到rod3上的最少花费。
PS:
dp[n][i][j]:表示把 n 个盘子从 rodi 移动到 rodj 上的最少花费。
决策有两个:
1. 将rod1上的n-1个盘子移动到rod2上,将rod1上的最后一个移动到rod3上,再将rod2上的n-1个盘子移动到rod3上。
2. 将rod1上的n-1个盘子移动到rod3上,将rod1上的最后一个移动到rod2上,将rod3上的n-1个盘子移动回rod1上,将rod2上的最后一个盘子移动到rod3上,最后将rod1上的n-1个盘子移动到rod3上。
dp[0][i][j] = 0, 其他置为无穷大.
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;#define LL long longint t[4][4];long long dp[47][4][4];int main(){ for(int i = 1; i <= 3; i++) { for(int j = 1; j <= 3; j++) { scanf("%d",&t[i][j]); } } int n; LL a, b; scanf("%d",&n); for(int k = 1; k <= n; k++) { for(int i = 1; i <= 3; i++) { for(int j = 1; j <= 3; j++) { if(i != j) { a = dp[k-1][i][6-i-j] + t[i][j] + dp[k-1][6-i-j][j]; b = dp[k-1][i][j] + t[i][6-i-j] + dp[k-1][j][i]+t[6-i-j][j] + dp[k-1][i][j]; dp[k][i][j] = min(a, b); } } } } cout<<dp[n][1][3]<<endl;;}
- codeforces 392 B. Tower of Hanoi(汉诺塔 DP)
- Codeforces 392B Tower of Hanoi(DP)
- Codeforces 392B Tower of Hanoi
- Tower of Hanoi CodeForces 392B
- codeforces 392B Tower of Hanoi 记忆化搜索
- CF:392B - Tower of Hanoi 记忆化搜索DP
- Codeforce 392B Tower of Hanoi 区间dp
- Codeforces 392B Tower of Hanoi(递归+记忆化搜索)
- codeforces Tower of Hanoi
- 【进阶的汉诺塔】code forces 392B Tower of Hanoi
- Tower of Hanoi(汉诺塔)
- Codeforces Round #230 (Div. 1) B. Tower of Hanoi
- Codeforces 101243 B Hanoi tower
- codeforces 230 div2 D Tower of Hanoi DP
- 汉诺塔 Tower of Hanoi
- Codeforces Round #230 (Div. 2) D. Tower of Hanoi (加强版汉诺塔,DP)
- CF 392 B. Tower of Hanoi 记忆化搜索
- codefores 392B Tower of Hanoi (记忆搜索)
- 统计学笔记
- 各种OJ资料规集
- 黑马程序员_java08_多线程
- 1072. Gas Station (30)
- TestNG 基本注解
- codeforces 392 B. Tower of Hanoi(汉诺塔 DP)
- 简单的35选7彩票系统
- Listview添加头和尾,addHeader,addFooter,以及利用尾显示更新过程
- C++之引用
- 第二周上机实践项目0—宣告“主权”,我想说的话
- 树的直径 hdu 4607
- map插入数据
- MVC模式简要介绍
- win10(win8应该也可以) intellij idea 配置文件移动