现在的位置: 首页 > 综合 > 正文

UVA 10626 – Buying Coke

2019年02月23日 ⁄ 综合 ⁄ 共 1119字 ⁄ 字号 评论关闭

题目链接~~>

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

解题思路:记忆化搜索

                开三维的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 ;
}

抱歉!评论已关闭.