您的位置:首页 > 其它

LightOJ - 1169 Monkeys on Twin Tower(记忆化搜索)

2015-10-31 23:27 281 查看
题目大意:有两座塔,每座塔的每层都有香蕉,给出吃完每层香蕉所需要的时间。在第i层的时候有两种选择,一种是跳到该塔的i+1层,花费时间为0,另一种是爬螺旋楼梯过去另一座塔的i+1层,需要花费时间t,现在问,要每层都吃到香蕉,至少需要发费多少时间

解题思路:用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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: