HDU 4597 Play Game

本文介绍了一个关于双端队列的博弈问题:两个玩家轮流从两个双端队列中取值,每次可以取队列头部或尾部的元素,目标是使自己的得分最大化。文章提供了一个C++实现的动态规划解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

有两个双端队列每次每人可以从任意一堆的头或尾取值,两人均采取最优策略,问先手的最后得分

很简单的博弈

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int inf = 1000000000;
int T, N, A[25], B[25], f[25][25][25][25];
int DP(int a, int b, int c, int d) {
    int turn = N - (b - a + 1) + N - (d - c + 1);
    if(turn == (N << 1)) return 0;
    int & res = f[a][b][c][d];
    if(res != -1) return res;
    if(turn & 1) {
        res = inf;
        if(a <= b) res = min(res, DP(a + 1, b, c, d));
        if(a <= b) res = min(res, DP(a, b - 1, c, d));
        if(c <= d) res = min(res, DP(a, b, c + 1, d));
        if(c <= d) res = min(res, DP(a, b, c, d - 1));
    }
    else {
        res = 0;
        if(a <= b) res = max(res, DP(a + 1, b, c, d) + A[a]);
        if(a <= b) res = max(res, DP(a, b - 1, c, d) + A[b]);
        if(c <= d) res = max(res, DP(a, b, c + 1, d) + B[c]);
        if(c <= d) res = max(res, DP(a, b, c, d - 1) + B[d]);
    }
    return res;
}
int main() {
    scanf("%d", &T);
    for(int kase = 1; kase <= T; kase++) {
        scanf("%d", &N);
        for(int i = 1; i <= N; i++) {
            scanf("%d", &A[i]);
        }
        for(int i = 1; i <= N; i++) {
            scanf("%d", &B[i]);
        }
        memset(f, -1, sizeof(f));
        printf("%d\n", DP(1, N, 1, N));
    }
    return 0;
}

转载于:https://www.cnblogs.com/ljzalc1022/p/9064423.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值