[HDU] 2616 Kill the monster 一种基本剪枝的暴力深搜

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2616

 方法:深搜树中每一个状态节点包含的信息为:使用武器的标号,怪物还剩余的生命值,以及该节点处于的层数也就是使用了多少个武器。然后分别以每一个武器作为树的根节点以实现暴力深搜。深搜的过程中用到一个很基本的剪枝方法,那就是在搜索过程中维护一个当前已经获取的最优解,该题目中为当前为了消灭怪物使用武器数量最下那个数量MinCount,然后每一个次搜索的时候发现如果再继续搜索很得到次于当前最有解的解,是不会被选择的解,于是再搜索便无意义,所以就剪掉,在该题目中,如果发现当前节点的层数已经大于当前MinCoun,剪掉。

感想:简单题

代码:

View Code
#include <iostream>
#include <queue>
#include<algorithm>
using namespace std;
int n,m;
const int MAX=0x7fffffff;
bool v[10];
struct Spell
{
    int injure;
    int boundary;
};
Spell spells[10];
struct Step
{
    int x;
    int currentRemain;
    int layer;
};
int currentMostFast =MAX;
int BFSSearch(Step step)
{
    int re=MAX;
    if(step.currentRemain<=0)
    {
        if(currentMostFast>step.layer)
            currentMostFast= step.layer;
        return step.layer;
    }
    v[step.x] = true;
    for(int i=0;i<n;i++)
    {
        if(!v[i])
        {
            Step n_step;
            n_step.x = i;
            n_step.layer =step.layer+1;
            n_step.currentRemain = step.currentRemain  -spells[n_step.x].injure *( step.currentRemain <= spells[n_step.x].boundary ? 2:1);
            if(n_step.layer<currentMostFast)
            {
                int t_re = BFSSearch(n_step);
                re = re > t_re ? t_re :re;
            }
        }
    }
    v[step.x] = false;
    return re;
}
int BFSSearch(int start)
{ 
    Step step; 
    step.x=start;
    step.layer=1;
    step.currentRemain = spells[start].boundary >= m ? m-spells[start].injure*2 :  m-spells[start].injure;
    return BFSSearch(step);;
}
 
void main()
{
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        int re =MAX,t_re;
        currentMostFast =MAX;
        for(int i=0;i<n;i++)
            scanf("%d %d",&spells[i].injure,&spells[i].boundary);
        for(int i=0;i<n;i++)
        {
            t_re = BFSSearch(i);
            re = re > t_re ? t_re : re;
        }
        cout<< (re == MAX ? -1 :re)<<endl;
    }
}
原文地址:https://www.cnblogs.com/kbyd/p/3034593.html