HDU 2616 Kill the monster

来源:互联网 发布:hbo 知乎 编辑:程序博客网 时间:2024/05/22 01:51

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.

InputThe 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).OutputFor 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 Input3 10010 2045 895  403 10010 2045 905 403 10010 2045 845 40Sample Output32-1AuthoryifenfeiSource奋斗的年代

一道數據量很小的常規DFS搜索題。

#include<iostream>#include<cstring>#include<string>#include<algorithm>using namespace std;#define ll long long int#define INF 0x3f3f3f3fint n; int m; int cnt;struct node{    int A;    int M;}a[15];int vis[15];void dfs(int c, int s) //s為怪物血量,c為當前步驟數{    if (s <= 0 && cnt > c)    {        cnt = c;        return;    }    if (c > cnt)//剪枝操作    {        return;    }    for (int i = 0; i < n; i++)    {        if (!vis[i])        {            vis[i] = 1;            int t = (s <= a[i].M ? s - 2 * a[i].A : s - a[i].A);            dfs(c + 1, t);            vis[i] = 0;        }    }}int main(){    while (cin >> n >> m)    {        memset(vis, 0, sizeof(vis));        for (int i = 0; i < n; i++)        {            cin >> a[i].A >> a[i].M;        }        cnt = INF;        dfs(0, m);        if (cnt == INF)        {            cout << "-1" << endl;        }        else        {            cout << cnt << endl;        }    }    return 0;}
原创粉丝点击