HDU 2616 Kill the monster(简单DFS)

 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2616

Kill the monster

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


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
 

 

Author
yifenfei
 

 

Source
 

 

Recommend
yifenfei
 
 
简单DFS搜索题。。还好1A了,不然真弱爆了。。。。
 
 By LFENG
 
#include<stdio.h>
#include<string.h>
struct spell
{
    
int effect;
    
int hp;
}sp[
30];
int n,m,mint,HP;
int vis[30];
void getdata()
{
    
int i;
    
for(i=0;i<n;i++)
    scanf(
"%d %d",&sp[i].effect,&sp[i].hp);
    mint=
9999999;
    memset(vis,
0,sizeof(vis));
}
void dfs(int num,int mhp)
{
    
int i;
    
if(mhp<=0)
    {
        
if(mint>num)mint=num;
        
return ;
    }
    
for(i=0;i<n;i++)
    {
        
if(vis[i])continue;
        vis[i]=
1;
        
if(mhp<=sp[i].hp)dfs(num+1,mhp-sp[i].effect*2);
        
else dfs(num+1,mhp-sp[i].effect);
        vis[i]=
0;
    }
}
int main()
{
    
int t;
    
while(scanf("%d %d",&n,&HP)!=EOF)
    {
        getdata();
        dfs(
0,HP);
        
if(mint<9999999)printf("%d\n",mint);
        
else printf("-1\n");
    }
    
return 0;
}
posted @ 2013-05-26 15:55  LFENG  阅读(263)  评论(0编辑  收藏  举报