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

题目大意:有两座塔,每座塔的每层都有香蕉,给出吃完每层香蕉所需要的时间。在第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][N], jump[2][N];
int dp[2][N];

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值