很简单的一道回溯搜索题目!!!、
#include<stdio.h>
#include<string.h>#define inf 99999999
int a[20],b[20],visit[20];
int n,m,min;
int dfs(int t,int hp)
{
int i;
if(t>n)
return -1;
if(hp<=0)
{
if(min>t)
min=t;
return min;
}
for(i=1;i<=n;i++)
if(visit[i]==0)
{
visit[i]=1;
if(hp<=b[i])
dfs(t+1,hp-2*a[i]);
else
dfs(t+1,hp-a[i]);
visit[i]=0;
}
return -1;
}
int main()
{
int ans,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
min=inf;
memset(visit,0,sizeof(visit));
for(i=1;i<=n;i++)
scanf("%d%d",&a[i],&b[i]);
ans=dfs(0,m);
if(min==inf)
printf("-1\n");
else
printf("%d\n",min);
}
return 0;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2616