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 DescriptionThere 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.
InputThe 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).
OutputFor 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 10010 2045 895 403 10010 2045 905 403 10010 2045 845 40
Sample Output
32-1
Authoryifenfei
Source奮鬥的年代
Recommendyifenfei
解題思路:深度優先搜尋題目,還要注意技能使用的順序,雖然有”最少“等字眼,但也不適合用廣搜,只要把怪獸殺死即可,技能循序不定,要求使用的最少技能數。
注意,技能使用不能超過給定的技能數(這裡讓我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;}