(DFS) Kill the monster -- HDOJ

来源:互联网 发布:美国大学学生会 知乎 编辑:程序博客网 时间:2024/06/05 20:44

Kill the monster
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 177 Accepted Submission(s): 128

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<iostream>#include<string.h>#include<string>#include<stdio.h>#include<algorithm>#include<queue>#include<math.h>#define PI acos(-1.0)#define eps 0.00000001using namespace std;typedef struct Node{    int a,m;}Node;int n,M,ans;Node spe[12];void DFS(int sum,int vis[],int num){ //   cout << sum <<endl;    if(sum <= 0)    {        ans = min(ans,num);        return;    }    for(int i=0; i<n; ++i)    {        if(!vis[i])        {            vis[i] = 1;            if(spe[i].m >= sum)                DFS(sum - 2*spe[i].a,vis,num+1);            else                DFS(sum - spe[i].a,vis,num+1);            vis[i] = 0;        }    }}main(void){//    freopen("in.txt","r",stdin);    while(scanf("%d %d",&n,&M) != EOF)    {        int vis[n];        memset(vis,0,sizeof(vis));        for(int i=0; i<n; ++i)        {            scanf("%d %d",&spe[i].a,&spe[i].m);        }        ans = 999999;        DFS(M,vis,0);        if(ans == 999999) ans = -1;        cout << ans <<endl;    }    return 0;}
原创粉丝点击