HDU 2616 Kill the monster【深搜】

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
分析:和N皇后问题一样,深搜找最优解。
code:
View Code
#include<stdio.h>
#include<string.h>
int a[11],b[11],vi[11];
int tot,n,v;
void dfs(int h,int k)
{
int i;
if(k>n)
return;
if(h<=0)
{
if(tot>k)
tot=k;
return;
}
for(i=0;i<n;i++)
{
if(!vi[i])
{
vi[i]=1;
if(h<=b[i])
dfs(h-2*a[i],k+1);
if(h>b[i])
dfs(h-a[i],k+1);
vi[i]=0;
}
}
}
int main()
{
int i;
while(scanf("%d%d",&n,&v)!=EOF)
{
tot=20;
for(i=0;i<n;i++)
scanf("%d%d",&a[i],&b[i]);
memset(vi,0,sizeof(vi));
dfs(v,0);
if(tot!=20)
printf("%d\n",tot);
else printf("-1\n");
}
return 0;
}

posted @ 2012-03-15 12:57  'wind  阅读(260)  评论(0编辑  收藏  举报