题目链接: D - Deck of Cards ZOJ - 2852
做的时候想太多了,不敢直接DP想先化简,就是不考虑插槽的情况尽可能多的完成操作四
其实不行,最好的方法就是直接上DP
dp[p].[i].[j].[k] 代表前p张牌时,三个插槽里面装的牌值分别为 i , j , k时的最大金额
转移方程比较简单,在代码里面吧
#include <cstdio>
#include <algorithm>
#include <memory>
#include <cstring>
#define JOKER 21
#define FACE 10
using namespace std;
const int MAX = 101;
int convert_to_val(char c){
if(c == 'F')return JOKER;
else if(c == 'A')return 1;
else if(c >= '2' && c <= '9')return c - '0';
else if(c == 'T' || c == 'J' || c == 'Q' || c == 'K')return FACE;
}
int dp[102][31][31][31];
int main(){
int N;
while(scanf("%d", &N) && N){
int ans = -1;
memset(dp, -1, sizeof(dp));
dp[0][0][0][0] = 0;
int f = 0;
for(int p = 1; p <= N+1; ++p){
char card[2];
if(p != N+1)
scanf("%s", card);
else
card[0] = 'F';
int val = convert_to_val(card[0]);
for(int i = 0; i < 31; ++i){
for(int j = 0; j < 31; ++j){
for(int k = 0; k < 31; ++k){
if(dp[p-1][i][j][k] == -1)continue;
ans = max(ans, dp[p-1][i][j][k]);
if(val == JOKER){//joker case
if(i <= 20)
dp[p][0][j][k] = max(dp[p][0][j][k], dp[p-1][i][j][k] + 50 + 100);
if(j <= 20)
dp[p][i][0][k] = max(dp[p][i][0][k], dp[p-1][i][j][k] + 50 + 200);
if(k <= 20)
dp[p][i][j][0] = max(dp[p][i][j][0], dp[p-1][i][j][k] + 50 + 300);
}else{
int nv = i + val;
if(nv == 21){//reach 21
dp[p][0][j][k] = max(dp[p][0][j][k], dp[p-1][i][j][k] + 50 + 100);
}else if(i < 21){
dp[p][nv][j][k] = max(dp[p][nv][j][k], dp[p-1][i][j][k] + 50);
}
nv = j + val;
if(nv == 21){//reach 21
dp[p][i][0][k] = max(dp[p][i][0][k], dp[p-1][i][j][k] + 50 + 200);
}else if(j < 21){
dp[p][i][nv][k] = max(dp[p][i][nv][k], dp[p-1][i][j][k] + 50);
}
nv = k + val;
if(nv == 21){//reach 21
dp[p][i][j][0] = max(dp[p][i][j][0], dp[p-1][i][j][k] + 50 + 300);
}else if(k < 21){
dp[p][i][j][nv] = max(dp[p][i][j][nv], dp[p-1][i][j][k] + 50);
}
}
}
}
}
}
printf("%d\n", ans);
}
return 0;
}