《算法竞赛从入门到进阶》心得及相关题解006--dfs--hdu2677


题目意思:hdu2677,dota游戏,你能在商店买到基本武器,手头上也有一些武器(不一定为基本武器),且知道武器的合成规则,现在给出想要的武器列表,问最少需要花多少钱。
解题思路:递归想要的武器列表: 1.如果手头上已有该武器想要的全部数量,则不购买返回0,如只有部分数量,则购买不足部分的数量返回; 2.如该武器为基本武器,则从商店购买返回; 3.如该武器不为基本武器,则根据武器合成规则递归算出需要的价钱。
易错的点:此题难度不大,但注意不要用map,之前全部用STL的map偷懒,结果MLE了好多次。
代码明细:
//probID: hdu2677
//author: WiselyQY
//date: 2020-12-29
#include <bits/stdc++.h>
using namespace std;

//能在商店买到的基本武器
struct Node1
{
    string name;
    int price;
}eq[105];
int eq_amount;
//已有武器
struct Node2
{
    string name;
    int amount;
}has[105];
int has_amount;
//武器合成规则
struct Node3
{
    string name;
    vector<string> part;
}rule[105];
int rule_amount;
//想要的武器
struct Node4
{
    string name;
    int amount;
}wanted[105];
int wanted_amount;

int dfs(string name, int amount)
{
    //先遍历是否已有该武器且该武器还没用完
    for(int i=0; i<has_amount; i++)
        if(has[i].name==name && has[i].amount)
        {
            //已有武器能满足,直接使用无需购买返回0
            if(has[i].amount>=amount) 
            {
                has[i].amount-=amount; 
                return 0;
            }
            //不能完全满足则购买不足的部分
            else
            {
                int tmp=has[i].amount;
                has[i].amount=0;
                return dfs(name,amount-tmp);
            }            
        }
    //已转换成能在商店买到的基本武器
    for(int i=0; i<eq_amount; i++)
        if(eq[i].name==name) return eq[i].price*amount;
    //不是基本武器,则根据武器合成规则递归算出需要多少钱
    int sum=0;
    for(int i=0; i<rule_amount; i++)
        if(rule[i].name==name)
        {
            for(int j=0; j<rule[i].part.size(); j++)
                sum+=dfs(rule[i].part[j],amount);
        }
    return sum;
}
int main()
{
    clock_t start,end;
    start=clock();
#ifndef ONLINE_JUDGE
    freopen(R"(D:\hdu\2677\in.txt)","r",stdin);
#endif
    while(cin>>eq_amount)
    {
        //分别输入基本武器、已有武器、武器规则和想要的武器
        for(int i=0; i<eq_amount; i++) cin>>eq[i].name>>eq[i].price;
        cin>>has_amount;
        for(int i=0; i<has_amount; i++) cin>>has[i].name>>has[i].amount;
        cin>>rule_amount;
        for(int i=0; i<rule_amount; i++)
        {
            rule[i].part.clear();
            string str1,str2;
            while(cin>>str1>>str2)
            {
                rule[i].part.push_back(str1);
                if(str2=="=") break;
            }
            cin>>rule[i].name;
        }
        cin>>wanted_amount;
        for(int i=0; i<wanted_amount; i++) cin>>wanted[i].name>>wanted[i].amount;
        
        //循环算出需要的武器的总价钱
        int ans=0;
        for(int i=0; i<wanted_amount; i++)        
            ans+=dfs(wanted[i].name, wanted[i].amount);
        cout<<ans<<endl;
    }
    end=clock();
    //printf("time=%lfs\n",(double)(end-start)/1000);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值