HDU 2616 Kill the monster (深搜DFS)

Kill the monster

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



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<n<10, 1<m<10^7), express how many spells yifenfei has.
Next n line , each line express one spell. (Ai, Mi).(0<Ai,Mi<=m).
 

Output
For each test case output one integer that how many spells yifenfei should use at least. If yifenfei can not kill the monster output -1.
 

Sample Input
  
  
3 100 10 20 45 89 5 40 3 100 10 20 45 90 5 40 3 100 10 20 45 84 5 40
 

Sample Output
3
2
-1
题解:德玛西亚去打大龙,德玛有N个技能,大龙有M点血,德玛的N个技能(后面N行)每个技能都有一个稳定的伤害值,但当大龙血量低于某一值时,会产生暴击,造成二倍伤害(后面N行每行两个数,第一个数,稳定伤害值,第二个数,产生暴击 的血量临近点)……

#include<stdio.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<queue>;
#include<stack>;
#include <iostream>
#include <queue>
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
int nn[12],mm[12];                       /*储存伤害值,和暴击临界值*/
bool v[12];                               /*标记……*/
int n,m,step;
void dfs(int m,int s)
{
    if(s>=step)return;                    /*剪枝,也就是优化……*/
    if(m<=0){step=s<step?s:step;return;}   /*记录用的技能数……*/
    for(int i=0;i<n;i++)
    {
        if(!v[i])
        {
            v[i]=true;
            if(m<=mm[i])dfs(m-2*nn[i],s+1); /*你们最喜欢的暴击来了……*/
            else dfs(m-nn[i],s+1);           /*普通伤害值……*/
            v[i]=false;
        }
    }
}
int main()
{
    while(cin>>n>>m)
    {
        mem(v);mem(nn);mem(mm);            /*初始化……*/
        for(int i=0;i<n;i++)
            cin>>nn[i]>>mm[i];
        step=12;
        dfs(m,0);
        if(step==12)
            cout<<"-1"<<endl;
        else cout<<step<<endl;
    }
}
笔记:可能上面的你们可能看不懂深搜具体如何进行的下面我给出一个我做过标记的代码,可以帮助理解……
#include<stdio.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<stack>
#include <queue>
using namespace std;
int nn[12],mm[12];
bool v[12];
int n,m,step,A,B;
void dfs(int m,int s)
{
    if(s>=step)
    {
return;
    }
    if(m<=0)
    {
        step=s<step?s:step;return;
    }
    for(int i=0;i<n;i++)
    {
        if(!v[i])
        {
            v[i]=1;
            if(m<=mm[i])
            {
               printf("A  %d  %d\n",A++,m-2*nn[i]);
                dfs(m-2*nn[i],s+1);
            }
            else
            {
                 printf("B  %d %d\n",B++,m-nn[i]);
                dfs(m-nn[i],s+1);
            }
            cout<<'i'<<i<<endl;
            v[i]=0;
        }
    }
        printf("s %d\n",s);
}
int main()
{
    while(cin>>n>>m)
    {
        memset(v,false,sizeof(v));
        memset(nn,0,sizeof(nn));
        memset(mm,0,sizeof(mm));
        for(int i=0;i<n;i++)
            cin>>nn[i]>>mm[i];
         step=20,A=0,B=0;
        dfs(m,0);
        if(step==20)
            cout<<"-1"<<endl;
        else
            cout<<step<<endl;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值