Test instructions: One by one 21-point game. 1. There are three stacks of cards, respectively, 1x,2x,3x. 2. The value of solitaire A is 1, the value of solitaire 2-9 is the same as the card, and the value of ten (T), J, Q, K is 10, while the value of joke (F) is arbitrarily large. 3. One by one cards are placed in three stacks in order. When the value of a pile is more than 21 points, it cannot be placed, and if the total value of a pile is 21, the queue will be emptied, and when the joke is placed on it, the stack is immediately changed to 21 points. 4. Successfully put one by one cards to $50; successfully emptied one by one stacks to speak of the 100* card heap number, that is, 1X to $100, 2X to 200 dollars, 3X to 300 dollars. 5. When no pile can continue to play cards, or have no cards, the game is over. Now ask one by one of the cards to put the maximum amount of dollars in some way. Idea: four-dimensional DP, so that DP[I][J][K][G] to show the first card, the value of the 1X heap is the value of the j,2x heap is the value of k,3x g, the maximum amount of money to get. In 1x, for example, set V as the value of the current card, whose transfer equation is dp[i+1][j+v][k][g] =max (Dp[i+1][j+v][k][g], dp[i][j][k][g]+50) when J < 21 and J + V! = 21 and V!=21. DP[I+1][0][K][G] = max (dp[i+1][0][k][g], dp[i][j][k][g]+50), when J <21 and V is joke, or j + v = = 21. Of course, you can use a scrolling array to reduce space consumption.
Solution by Sake
Source Code:
//#pragma COMMENT (linker, "/stack:16777216")//For C + + Compiler#include <stdio.h>#include<iostream>#include<fstream>#include<cstring>#include<cmath>#include<stack>#include<string>#include<map>#include<Set>#include<list>#include<queue>#include<vector>#include<algorithm>#defineMax (b) ((a) > (b))? (a): (b))#defineMin (b) ((a) < (a))? (a): (b))#defineAbs (x) (((x) > 0)? (x): (-(x)))#defineMOD 1000000007#definePi ACOs (-1.0)using namespaceStd;typedefLong Longll; typedef unsignedLong Longull; typedef unsignedint UINT; typedef unsignedCharUchar; template<classT> InlinevoidCheckmin (T &a,t b) {if(a>b) a=b;} Template<classT> InlinevoidCheckmax (T &a,t b) {if(a<b) a=b;}Const DoubleEPS = 1e-7 ;Const intN = About ;Const intM =1100011*2 ;Constll P =10000000097ll;Const intMAXN =10900000 ;intdp[ -][ +][ +][ +], n, ans;CharC;intGetValue (Charc) {if(c >='2'&& C <='9') { returnC'0'; } Else { if(c = ='A')return 1; if(c = ='T')return Ten; if(c = ='J')return Ten; if(c = ='Q')return Ten; if(c = ='K')return Ten; if(c = ='F')return 0; } return 0;}intMain () {Std::ios::sync_with_stdio (false); intI, J, T, K, U, V, g, numcase =0; while(Cin >>N) {if(0= = N) Break; Memset (DP,-1,sizeof(DP)); dp[0][0][0][0] =0; Ans=0; for(i =0; I <= N; ++i) {if(I <N) {cin>>C; } Else{C=0; } v=GetValue (c); for(j =0; J < to; ++j) { for(k =0; K < to; ++k) { for(g =0; G < to; ++g) {if(Dp[i][j][k][g] = =-1)Continue; Checkmax (ans, dp[i][j][k][g]); if(v = =0&& J < +) || J + V = = +) {Checkmax (dp[i+1][0][K][G], Dp[i][j][k][g] + Max); } Else if(J < +) {Checkmax (dp[i+1][j + v][k][g], Dp[i][j][k][g] + -); } if(v = =0&& K < +) || K + v = = +) {Checkmax (dp[i+1][j][0][G], Dp[i][j][k][g] + -); } Else if(K < +) {Checkmax (dp[i+1][j][k + v][g], Dp[i][j][k][g] + -); } if(v = =0&& G < +) || G + V = = +) {Checkmax (dp[i+1][j][k][0], Dp[i][j][k][g] + -); } Else if(G < +) {Checkmax (dp[i+1][j][k][g + v], Dp[i][j][k][g] + -); }}}} memset (Dp[i],-1,sizeof(Dp[i])); } cout<< ans <<Endl; } return 0;}
ZOJ 2852 Deck of Cards DP