Kill the monster
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1223 Accepted Submission(s): 839
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
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int ai,mi;
} a[20];
int n,m,sum;
int book[12];
void dfs(int num,int HP)
{
if(num>=sum)
{
return;//如果此时总数大于已知的最小的总数,则不需再往下判断
}
if(HP<=0)
{
if(num<sum)//如果又另一种使用更少的武器的方法,使用武器的数量更新为更少的
{
sum=num;
return ;
}
}
for(int i=0; i<n; i++)
{
if(book[i]==0)
{
book[i]=1;
if(HP<=a[i].mi)
dfs(num+1,HP-2*a[i].ai);
else dfs(num+1,HP-a[i].ai);
book[i]=0;//回溯
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(book,0,sizeof(book));
for(int i=0; i<n; i++)
{
scanf("%d %d",&a[i].ai,&a[i].mi);
}
sum=12;//因为最大为12,如果sum的值不更新,则不能把此怪兽打死
dfs(0,m);
if(sum==12)
{
printf("-1\n");
}
else printf("%d\n",sum);
}
return 0;
}