Kill the monster

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


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


解题思路:深度优先搜索题目,还要注意技能使用的顺序,虽然有”最少“等字眼,但也不适合用广搜,只要把怪兽杀死即可,技能循序不定,要求使用的最少技能数。
注意,技能使用不能超过给定的技能数(这里让我WA了一次,伤心),每个技能不能多次使用。技能使用的顺序直接影响杀死怪兽所需技能数,因为,A,B差值的原因。




#include<cstdio> #include<cstring> #include<algorithm> int n,M; int map[11]; int min1=12; struct node {     int a;     int m; }spell[11]; void dfs(int x) {     int i,j;     if(M<=0)    //怪兽死了     {         min1=min1<x?min1:x;    //最小技能数存储         //printf("min1=%d x=%d\n",min1,x);         return ;     }     else if(x==n)//易错点:x不能大于n   //这样就不会使用“虚有”的技能啦         return ;     for(i=0;i<n;i++)     //顺序哦,也是从素数环那个题目那里学来的     {         if(!map[i])         {             map[i]=1;             j=spell[i].a;             if(M<=spell[i].m)                 j+=spell[i].a;     //双倍血量,影响加倍哦             M-=j;             dfs(x+1);             map[i]=0;             M+=j;         }     } } int main() {     int i,j;     while(scanf("%d%d",&n,&M)!=EOF)     {         memset(map,0,sizeof(map));         min1=12;         for(i=0;i<n;i++)             scanf("%d%d",&spell[i].a,&spell[i].m);         dfs(0);         if(min1!=12)             printf("%d\n",min1);         else             printf("-1\n");     }     return 0; }