hd2616 Kill the monster

Kill the monster

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


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 Next n line , each line express one spell. (Ai, Mi).(0
 

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次下打死怪物,规定:技能的AI为攻击伤害,MI为如果怪物的血量小于MI,此处的攻击伤害加倍。
解题思路:深搜,cot 用于递归技能次数,m为怪物的血量,每次递归cot+1,m减去相应的血量,当m<=0时即为打死怪物,此时比较寻找最少技能次数。最终返回一个最小次数。
#include 
#include 
#include 
int n,m,a[11][2],cot,visit[11],sum,min;
int dfs(int cot,int m)//cot记录攻击次数,m为怪物血量
{
    int i;
    if(m <= 0)//当怪物血量为零时,寻找最小次数
    {
        min = min > cot ? cot : min;
    }
    for(i=1;i<=n;i++)//遍历数组
    {
        if(!visit[i])//是否用过
        {
             visit[i] = 1;//标记
            if(m <= a[i][1])//如果怪物血量小于技能的MI值,攻击伤害为2倍
                dfs(cot+1,m-2*a[i][0]);//所以递归将血量减去2倍的攻击伤害
            else
               dfs(cot+1,m-a[i][0]);//否则减去相应的血量即可
           visit[i]  = 0;//回溯
        }
    }
return min;}
int main()
{
    int i;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(visit,0,sizeof(visit));
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i][0],&a[i][1]);
        }
        min = 9999;
        if(dfs(0,m) == 9999)//如果min没有改变说明所有的技能都用完怪物的血量没有为0,即输出-1
            printf("-1\n");
        else
            printf("%d\n",min);//否则输出深搜得出的最小次数即可
    }
    return 0;
}


你可能感兴趣的:(hduoj,------搜索-------)