HDU 2616(Kill the monster )

/*简单DFS
题目链接地址:http://acm.hdu.edu.cn/showproblem.php?pid=2616
*/

#include<iostream>
using namespace std;
#define maxn 11
#define INF 0x3f3f3f3f
int arr[maxn],w[maxn],n,m,count;
bool flag,visited[maxn];
void dfs(int i,int rest,int num)
{
    if(flag && num >= count)  return;
    if(num > n)  return;
    if(rest <= 0)
    {
        if(num < count)
            count = num;
        flag = 1;
        return;
    }
    visited[i] = true;
    for(int j = 1; j <= n; j++)
    {
        if(!visited[j])
        {
            if(rest <= w[j])
                dfs(j,rest-2*arr[j],num+1);
            else
                dfs(j,rest-arr[j],num+1);
        }
    }
    visited[i] = false;
}
int main()
{
    //freopen("1012.txt","r",stdin);
    while(scanf("%d%d",&n,&m) != EOF)
    {
        int i;
        for(i = 1; i <= n; i++)
            scanf("%d%d",&arr[i],&w[i]);
        count = INF;
        for(i = 1; i <= n; i++)
        {
            memset(visited,false,sizeof(visited));
            flag = 0;
            if(w[i] >= m)  dfs(i,m-2*arr[i],1);
            else dfs(i,m-arr[i],1);
        }
        if(count == INF)  printf("-1\n");
        else printf("%d\n",count);
    }
    return 0;
}

 

 

posted @ 2012-10-14 22:56  sorryhao  阅读(186)  评论(0编辑  收藏  举报