HDU-2616-Kill the monster

A - Kill the monster
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

HDU 2616
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

题意:给出魔法数量,魔王血量。输入的一组魔法数值中,第一个数是普攻数值,当血量小于等于第二个数值时,普攻会变成暴击,造成二倍普攻伤害,当魔王血量小于等于0时,输出用到的最少魔法数量,如果没有把魔王干掉,就输出-1;

全排列后暴力枚举所有的可能,然后逐个搜索,同时记录下最少的次数。
懒省劲的我用的全排列函数实现。

这题和http://blog.csdn.net/qq_32680617/article/details/50815113思路方法都一样,只是这题需要单独开设一个用于全排列的数组

代码

#include<stdio.h>
#include<string.h>
#include<string>
#include<stack>
#include<queue>
#include<math.h>
#include<limits.h>
#include<malloc.h>
#include<iostream>
#include<algorithm>
using namespace std;
//以后不要改头文件
int main()
{
    int n;//魔法数量
    long long int blood;//总血量
    while(scanf("%d%lld",&n,&blood)!=EOF)
    {
        int attack[11];//普攻数值
        int crit[11];//暴击要求
        int flag[11];//全排列数组
        for(int i=0; i<n; i++)
        {
            flag[i]=i;//初始化全排列数组
            scanf("%d%d",&attack[i],&crit[i]);//接收数据
        }
        int num=100;//假定攻击次数为100,实际肯定小于10
        int bloods;//血量数值的副本
        int nums;//攻击次数的副本
        do
        {
            bloods=blood;//拷贝血量数值,防止后续改变血量数值
            nums=0;//暂时存储攻击次数
            for(int i=0; i<n; i++)
            {
                nums++;
                if(bloods<=crit[flag[i]])
                    bloods=bloods-attack[flag[i]]*2;
                else
                    bloods=bloods-attack[flag[i]];
                if(bloods<=0)
                {
                    num=min(num,nums);
                    break;
                }
            }
        }
        while(next_permutation(flag,flag+n));//这里用了全排列函数
        if(num==100)
            printf("-1\n");
        else
            printf("%d\n",num);
    }
    return 0;
}

等这个搜索小练做完,我再总结一下搜索的题。

你可能感兴趣的:(搜索)