HDU 2616 Kill the monster【全文翻译+详解】(广度优先搜索)

61 篇文章 0 订阅

Kill the monster

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1223 Accepted Submission(s): 839

Problem Description
There is a mountain near yifenfei’s hometown. On the mountain lived a big monster. As a hero in hometown, yifenfei wants to kill it.
Now we know yifenfei have n spells, and the monster have m HP, when HP <= 0 meaning monster be killed. Yifenfei’s spells have different effect if used in different time. now tell you each spells’s effects , expressed (A ,M). A show the spell can cost A HP to monster in the common time. M show that when the monster’s HP <= M, using this spell can get double effect.

Input
The input contains multiple test cases.
Each test case include, first two integers n, m (2

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct node
{
    int ai,mi;
} a[20];
int n,m,sum;
int book[12];
void dfs(int num,int HP)
{
    if(num>=sum)
    {
        return;//如果此时总数大于已知的最小的总数,则不需再往下判断
    }
    if(HP<=0)
    {
        if(num<sum)//如果又另一种使用更少的武器的方法,使用武器的数量更新为更少的
        {
            sum=num;
            return ;
        }
    }
    for(int i=0; i<n; i++)
    {
        if(book[i]==0)
        {
            book[i]=1;
            if(HP<=a[i].mi)
                dfs(num+1,HP-2*a[i].ai);
            else dfs(num+1,HP-a[i].ai);
            book[i]=0;//回溯
        }
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(book,0,sizeof(book));
        for(int i=0; i<n; i++)
        {
            scanf("%d %d",&a[i].ai,&a[i].mi);
        }
        sum=12;//因为最大为12,如果sum的值不更新,则不能把此怪兽打死
        dfs(0,m);
        if(sum==12)
        {
            printf("-1\n");
        }
        else printf("%d\n",sum);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值