hdu2616Kill the monster 深搜

来源:互联网 发布:mac桌面卸载图标 编辑:程序博客网 时间:2024/05/29 14:31

题目大意是一个人要杀死一个怪物,怪物的血量HP。有n种方式,每种对怪物的伤害值不同。而且当怪物的HP小于等于某一值时,会造成double伤害。

搜索分两次,每次先对当前伤害做一次,然后dfs下一次伤害。感觉还是没有体会到dfs的核心。如何建立搜索的过程太模糊了

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<vector>#include<cmath>#include<cctype>using namespace std;int n,m,temp,MIN;struct node {    int spell;    int HP;    int vit;};node a[12];void dfs(int dep,int hp){    if(dep>MIN)    return ;    if(dep >= n)    return ;    if(dep <= n-1&&hp<=0)    {        MIN=dep;        return ;    }    for(int i=0;i<n;i++)        if(!a[i].vit)        {            int flag=hp;            if(hp<=a[i].HP)                hp-=a[i].spell*2;            else                hp-=a[i].spell;            a[i].vit=1;            dfs(dep+1,hp);            hp=flag;            a[i].vit=0;        }}int main(){    int x,y;    while(cin>>n>>m)    {        for(int i=0;i<n;i++)        {            cin>>x>>y;            a[i].spell=x;            a[i].HP=y;            a[i].vit=0;        }        MIN=0xfff;        int flag = m;        for(int i=0;i<n;i++)        {            a[i].vit=1;            if(a[i].HP>=m)            m-=2*a[i].spell;            else            m-=a[i].spell;            dfs(0,m);            a[i].vit=0;            m=flag;        }        if(MIN!=0xfff)        cout<<MIN+1<<endl;        else        cout<<"-1"<<endl;    }}

原创粉丝点击