UVA 10626 - Buying Coke

题目链接~~>

做题感悟:做小白书后面的题目感觉大部分动态规划都可以用记忆化搜索去解决,记忆化搜索也很强大啊!

解题思路:记忆化搜索

                开三维的dp[ i ] [ j ] [ k ] 代表硬币分别还剩 i , j , k 的最优解.

这里投放硬币一定要注意:

            (1) 、 投放 一个 10 找回 两 个 1 

            (2) 、 投放一个 10 三个 1 找回一个 5 

            (3)、  投放两个 5 找回两个 1

            (4)、  投放一个 5 三个 1

            (5)、  投放八个 1

代码:

#include<iostream>
#include<fstream>
#include<iomanip>
#include<ctime>
#include<fstream>
#include<sstream>
#include<stack>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#define INT __int64
using namespace std ;
const double esp = 0.00000001 ;
const int INF = 0x3f3f3f3f ;
const int mod = 1e9 + 7 ;
const int MY = 10 + 10 ;
const int MX = 59049+ 10 ;
int n ,x ,y ,z ;
int dp[710][160][60] ;
int DP(int n ,int x ,int y ,int z)
{
    if(dp[x][y][z] != -1)
                     return dp[x][y][z] ;
    if(!n)   return 0 ;
    if(!y && !z) // 花费八个1 
                 return  dp[x][y][z] = n*8 ;
    int& ans = dp[x][y][z] ;
    ans = INF ;
    if(z >= 1)
    {
        ans = min(ans ,DP(n-1 ,x+2 ,y ,z-1) + 1) ; // 花费一个 10
        if(x >= 3)
           ans = min(ans ,DP(n-1 ,x-3 ,y+1 ,z-1) + 4) ; // 花费 1 个 10 3 个 1
    }
    if(y >= 2)
         ans = min(ans ,DP(n-1 ,x+2 ,y-2 ,z) + 2) ; // 花费 2 个 5 
    if(y >= 1 && x >= 3)
         ans = min(ans ,DP(n-1 ,x-3 ,y-1 ,z) + 4) ;  //花费 1 个 5 三个 1 
    return   ans ;
}
int main()
{
    int Tx ;
    scanf("%d" ,&Tx) ;
    while(Tx--)
    {
        scanf("%d%d%d%d" ,&n ,&x ,&y ,&z) ;
        memset(dp ,-1 ,sizeof(dp)) ;
        cout<<DP(n ,x ,y ,z)<<endl ;
    }
    return 0 ;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linux猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值