出现了一点问题,题目不能复制过来,主要的大意是有4堆糖果,有一个篮子,这个篮子最多可以装5个糖果,从4堆糖果堆的顶部每次取一个糖果,如果篮子中有颜色相同的一对糖果那么就可以装进口袋里,否则口袋达到5个后就结束游戏。
https://vjudge.net/problem/UVA-10118
上代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#define max(a,b) (((a)>(b))?(a):(b))
using namespace std;
const int maxn = 42;
int n;
int G[maxn][4];
int top[4];
int dp[maxn][maxn][maxn][maxn];
int match[22];
int dfs (int length , int *match) {
if (dp[top[0]][top[1]][top[2]][top[3]] != -1)
return dp[top[0]][top[1]][top[2]][top[3]];
if (length == 5)
return dp[top[0]][top[1]][top[2]][top[3]] = 0;
int ans = 0;
for (int i = 0 ; i < 4 ; i++) {
if (top[i] >= n)
continue;
int tc = G[top[i]][i];
top[i]++;
if(!match[tc]) {
match[tc] = 1;
ans = max(ans , dfs(length+1 , match));
match[tc] = 0;
}else {
match[tc] = 0;

博客讲述了如何解决UVA在线判题平台上的10118题"Free Candies"。问题涉及到从四堆糖果中按特定规则取糖,篮子容量限制为5个,并且有颜色匹配条件。博主分享了具体的动态规划解决方案和编程实现。
最低0.47元/天 解锁文章
799

被折叠的 条评论
为什么被折叠?



