POJ 1440-Varacious Steve (DP), poj1440-varacious
Steve and Digit take turns to take n doughnuts, each of which cannot exceed m (1 <= m <= n <= 100). Their game rules are, steve picked it first, and then everybody took the donut and put it on his own. When someone took the last one, he ate his own donut and took out all his opponent's donut, repeat this process. When a person eats the opponent, the first round of the game is the opponent's first hand, asking Steve to eat the most donut.
D [I] [j] [u] [0] indicates that there are currently a total of I donuts, j of which have not been retrieved yet, and u are at Steve, and when Steve leaves, Steve can eat the most donut. He uses d [I] [j] [u] [1] to indicate that there are a total of I and j are not retrieved, u is at Steve, and when Digit is next, Digit can eat the most donut.
State transition equation:
D [I] [j] [u] [0] = max {I-d [I] [j-v] [u + v] [1]}, i-d [I-j-u] [I-j-u] [0] [1] (if this step is completed, all the rest will be obtained )}
D [I] [j] [u] [1] = max {I-d [I] [j-v] [u] [0]}, i-d [I-j-q] [I-j-q] [0] [1] (if this step is completed, all the rest will be obtained )} (q = I-j-u, indicating the donut that Digit holds)
#include<stdio.h>#include<stdlib.h>int d[103][103][103][2];int main(void){int i,j,u,v,p,q,m,n,maxp;while(scanf("%d%d",&n,&m)==2){d[0][0][0][0]=d[0][0][0][1]=0;for(i=1;i<=n;i++){for(j=1;j<=i;j++){p=i-j;for(u=0;u<=p;u++){q=i-j-u;maxp=0;for(v=1;v<=m;v++){if(v>j){break;}else if(v==j){if(i-d[i-u-v][i-u-v][0][1]>maxp){maxp=i-d[i-u-v][i-u-v][0][1];}}else{if(i-d[i][j-v][u+v][1]>maxp){maxp=i-d[i][j-v][u+v][1];}}}d[i][j][u][0]=maxp;maxp=0;for(v=1;v<=m;v++){if(v>j){break;}else if(v==j){if(i-d[i-q-v][i-q-v][0][0]>maxp){maxp=i-d[i-q-v][i-q-v][0][0];}}else{if(i-d[i][j-v][u][0]>maxp){maxp=i-d[i][j-v][u][0];}}}d[i][j][u][1]=maxp;}}}printf("%d\n",d[n][n][0][0]);}return 0;}