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): 778    Accepted Submission(s): 556

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   |   We have carefully selected several similar problems for you:  
2614 
1426 
1258 
2660 
2514   

 

 1 //109MS    228K    694 B    G++    姜伯约
 2 /*
 3 
 4     题意:
 5         有n组数据,和怪兽血量m,每组数据有两个数,第一个为普通伤害值,
 6     第二个为怪兽血量少于该值时将造成双倍伤害,,求最少攻击次数,
 7     杀不死则输出-1.
 8     
 9     DFS:
10         比较明显的dfs,时间复杂度为O(n!),数据比较小而且不强,可以直接DFS
11     过了 
12 
13 */
14 #include<stdio.h>
15 #include<string.h>
16 int n,m;
17 int a[10][2];
18 int vis[10];
19 int cnt;
20 void dfs(int c,int s)
21 {
22     if(s<=0 && c<cnt){
23         cnt=c;
24         return;
25     }
26     if(c>=cnt) return;
27     for(int i=0;i<n;i++)
28         if(!vis[i]){
29             vis[i]=1;
30             int temp=(s<=a[i][1]?s-2*a[i][0]:s-a[i][0]);
31             dfs(c+1,temp);
32             vis[i]=0;
33         }
34         
35 }
36 int main(void)
37 {
38     while(scanf("%d%d",&n,&m)!=EOF)
39     {
40         for(int i=0;i<n;i++)
41             scanf("%d%d",&a[i][0],&a[i][1]);
42         memset(vis,0,sizeof(vis));
43         cnt=10;
44         dfs(0,m);
45         if(cnt==10) puts("-1");
46         else printf("%d\n",cnt);
47     }
48 }

 

    原文作者:heaventouch
    原文地址: https://www.cnblogs.com/GO-NO-1/p/3576370.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞