Game of Sum(动态规划)

本文介绍了一种使用动态规划解决两人博弈问题的方法,针对一串数组进行划分,通过计算后手玩家的最劣最优策略来确定先手玩家的最大得分。

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

题目:

给出一串数组,两人博弈,统计两人拿到的总数,求先手赢对手的最大值.

这道题小白上也有,做的时候觉得有做过类似的再见跑回去找找看,后来发现是一样的.不过第一次做的时候没理解透,觉得样例输出不是最优解再见,今天才真的明白.

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <algorithm>
using namespace std;

const int MAXN = 110;
int dp[MAXN][MAXN];
int a[MAXN], sum[MAXN];
int n;
int main() {
	while(scanf("%d", &n) != EOF && n) {
		for(int i=1; i<=n; i++) {
			scanf("%d", &a[i]);
			sum[i] = sum[i-1] + a[i];
		}
		memset(dp, 0, sizeof(dp));
		for(int i=1; i<=n; i++) dp[i][i] = a[i];
		for(int i=2; i<=n; i++) 
			for(int j=1, k=i; k<=n; k++, j++) {
				int ans = sum[k] - sum[j-1];
				for(int r=j; r<k; r++) {
					int m = min(dp[j][r], dp[r+1][k]);//后手最优最小值
					m = sum[k] - sum[j-1] - m;//先手最优值(基于总数一定)
					if(ans < m) ans = m;
				}
				dp[j][k] = ans;
			}
		printf("%d\n", 2*dp[1][n]-(sum[n]));//同样基于总数一定(先手取了最优,则余下为后手所得)
	}
	return 0;
}
思路:

主要的思路就是求出后手最劣的最优,既 m = min(dp[j][r], dp[r+1][k])所有区间基于次级区间,最终都将取完,既m = sum[k]-sum[j-1]-m;

Sure, here's a possible Python implementation of the game: ```python import random def roll_dice(num_dice): """Rolls a given number of dice and returns their values.""" return [random.randint(1, 6) for _ in range(num_dice)] def play_round(num_players): """Plays a round of Ship of Fools with a given number of players.""" scores = [0] * num_players for i in range(num_players): print(f"Player {i+1}'s turn:") input("Press Enter to roll the dice...") dice = roll_dice(5) print(f"You rolled: {dice}") if 6 in dice and 5 in dice and 4 in dice: print("You have a ship, captain, and crew! Good job.") cargo = sum(d for d in dice if d not in [6, 5, 4]) scores[i] = cargo else: print("Sorry, you don't have a ship, captain, and crew.") max_score = max(scores) winners = [i+1 for i, score in enumerate(scores) if score == max_score] print(f"\nRound over. The highest cargo was {max_score}.") print(f"Player {winners[0]} is the winner!\n") return winners[0] def play_game(num_players): """Plays a full game of Ship of Fools with a given number of players.""" print(f"\nStarting a game of Ship of Fools with {num_players} players...\n") winner = None while winner is None: winner = play_round(num_players) print(f"Game over. Player {winner} wins!\n") # Example usage: play_game(3) ``` In this implementation, the `roll_dice` function generates a list of random values between 1 and 6, representing the dice rolls. The `play_round` function simulates one round of the game for a given number of players. Each player takes a turn rolling the dice and trying to get a ship, captain, and crew. If successful, their score for the round is the sum of the remaining dice values. If unsuccessful, their score for the round is zero. After all players have taken their turns, the winner of the round is the one with the highest score. The `play_game` function plays a full game of Ship of Fools with a given number of players, continuing until one player wins two rounds.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值