hdu 2616 Kill the monster

Kill the monster

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 16   Accepted Submission(s) : 12

Font: Times New Roman | Verdana | Georgia

Font Size:  

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
直接深搜,找出步数最小的,但要注意回溯
#include<iostream>
using namespace std;
int n,m,step,flag;
int map[12];
struct ss
{
    int A,M;
}node[12];
void dfs(int num,int leave)
{
    if(num>n)             //剪枝
    {
        return ;
    }
    if(leave<=0)
    {
        flag++;
        if(step>num)
        {
            step=num;       //当前的符合和条件的跟以前最小的比较,取最小的
        }
        return ;
    }
    for(int i=0;i<n;i++)
    {
        if(!map[i])
        {
            map[i]=1;                              //标记
            if(leave<=node[i].M)
            {
                dfs(num+1,leave-2*node[i].A);
            }
            else
            {
                dfs(num+1,leave-node[i].A);
            }
            map[i]=0;                                  //回溯
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(map,0,sizeof(map));
        for(int i=0;i<n;i++)
        {
                scanf("%d%d",&node[i].A,&node[i].M);    
        }
        step=n;
        flag=0;
        dfs(0,m);
        if(flag==0) printf("-1\n");
        else printf("%d\n",step);
    }

    return 0;
}


posted @ 2013-07-24 20:15  ゐ星落★孤晨ね  阅读(134)  评论(0编辑  收藏  举报