LightOJ - 1169 Monkeys on Twin Tower(记忆化搜索)
2015-10-31 23:27
281 查看
题目大意:有两座塔,每座塔的每层都有香蕉,给出吃完每层香蕉所需要的时间。在第i层的时候有两种选择,一种是跳到该塔的i+1层,花费时间为0,另一种是爬螺旋楼梯过去另一座塔的i+1层,需要花费时间t,现在问,要每层都吃到香蕉,至少需要发费多少时间
解题思路:用dp[i][j]表示爬到第i座塔的第j层,吃到后面每层香蕉需要花费的最短时间,接着就是记忆化搜索了
解题思路:用dp[i][j]表示爬到第i座塔的第j层,吃到后面每层香蕉需要花费的最短时间,接着就是记忆化搜索了
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int INF = 0x3f3f3f3f; const int N = 1010; int n, cas = 1; int tower[2] , jump[2] ; int dp[2] ; void init() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &tower[0][i]); for (int i = 1; i <= n; i++) scanf("%d", &tower[1][i]); for (int i = 1; i < n; i++) scanf("%d", &jump[0][i]); for (int i = 1; i < n; i++) scanf("%d", &jump[1][i]); } int dfs(int tow, int floor) { if (~dp[tow][floor]) return dp[tow][floor]; if (floor == n) return tower[tow][floor]; int ans = INF; ans = min(ans, dfs(tow, floor + 1)); ans = min(ans, dfs(tow ^ 1, floor + 1) + jump[tow][floor]); return dp[tow][floor] = ans + tower[tow][floor]; } void solve() { memset(dp, -1, sizeof(dp)); printf("Case %d: %d\n", cas++, min(dfs(0, 1), dfs(1, 1))); } int main() { int test; scanf("%d", &test); while (test--) { init(); solve(); } return 0; }
相关文章推荐
- 一个SQL注入绕过分析(来源swpu web700)
- 电子阅读器
- “关系运算符”断案,打印杨辉三角
- win10(企业英文版)有关中文乱码 与 系统默认语言转换
- CentOS为firefox安装flash插件
- 《HBase权威指南》读书笔记10:第十章 集群监控
- HDU 5523 Game(就是情况比较多,讨论全了就可以了)——BestCoder Round #61(div.1 div.2)
- 怎么修改Xampp中Apache服务器的端口号
- 定义仓储
- 螺旋的矩阵
- MOOC PTA 07-图5 Saving James Bond - Hard Version
- hdoj4722Good Numbers【找规律】
- ios 多线程
- 今天的学习和生活2015/10/31
- 热爱
- Scrum Meeting 10.31
- JS正则表达式验证账号、手机号、电话和邮箱
- java代理模式
- dede头部和导航连接的更改
- sync fsync fdatasync