Kill the monster HDU - 2616

Kill the monster

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


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张符,你要用这n张符来杀死怪物,问你最少用几张可以杀死,如果杀不死就输出-1。符的使用方法是如果怪物的血量少于m,那就让怪物掉双倍a的血量值,否则只掉a的血量值

#include<stdio.h>
#include<map>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
struct node
{
    int a,m;
} s[15];
int vis[15];
int minn,n;
void dfs(int x,int cnt)
{
    if(x<=0)
    {
        minn=min(cnt,minn);
        return ;
    }
    for(int i=0; i<n; i++)
    {
        if(vis[i]==0)
        {
            vis[i]=1;
            if(x<=s[i].m)
                dfs(x-2*s[i].a,cnt+1);
            else
                dfs(x-s[i].a,cnt+1);
            vis[i]=0;//二了一点,打的时候没注意把这个条件放在if外边了,找了有一回bug和难受
        }
    }
}
int main()
{
    int mm;
    while(~scanf("%d%d",&n,&mm))
    {
        for(int i=0; i<n; i++)
            scanf("%d%d",&s[i].a,&s[i].m);
        memset(vis,0,sizeof(vis));
        minn=inf;
        dfs(mm,0);
        if(minn==inf)
            printf("-1\n");
        else
            printf("%d\n",minn);
    }
}

猜你喜欢

转载自blog.csdn.net/zezzezzez/article/details/81034959