记忆化搜索,不过要注意一句话As soon as I press the coke button (after having inserted sufficient amount of money), I receive a coke followed by the exchange (if any).
所以还有一种情况是先投入3个1 再投入一个10 ,这样找一个5
#include<iostream>#include<stdio.h>#include<string.h>#include<stdlib.h>usingnamespace std;#define INF 0x1f1f1fint num[5];int dp[800][111][111];intsolve(int i,int j,int k,int cnt){//i->1, j->5, k->10if(dp[i][j][k]>=0)return dp[i][j][k];if(cnt ==0)return dp[i][j][k]=0;int ans = INF;if(k >=1)
ans =min(ans,solve(i +2, j, k-1, cnt -1)+1);if(i >=8)
ans =min(ans,solve( i -8, j , k, cnt -1)+8);if(i >=3&& j >=1)
ans =min(ans,solve( i -3, j -1, k, cnt -1)+4);if(j >=2)//投2->5
ans =min(ans,solve( i +2, j -2, k , cnt -1)+2);if( k >=1&& i >=3)//先投入3,再投10
ans =min(ans,solve(i -3, j +1, k -1, cnt -1)+4);return dp[i][j][k]= ans;}intmain(){int t, c;scanf("%d",&t);while(t--){scanf("%d",&c);for(int i =0; i <3; i++)scanf("%d",&num[i]);memset(dp,-1,sizeof(dp));int ans =solve( num[0], num[1], num[2], c);printf("%d\n",ans);}}
记忆化搜索,不过要注意一句话As soon as I press the coke button (after having inserted sufficient amount of money), I receive a coke followed by the exchange (if any). 所以还有一种情况是先投入3个1 再投入一个10 ,这样找一个5