杭电2616Kill the monster(BFS过)(标记结构体解法)


Kill the monster

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1127    Accepted Submission(s): 788


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<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)但是搞了搞发现很难控制血量的伤害是双倍与单倍的问题 ~于是就慢慢放弃了这种解法 还是走上了老路~

搜索.

其实这个题目用深搜还是比较好理解的 而且数据也并不大 一定不会涉及到超时的问题~

~但是捏 我还是比较喜欢广搜来解题~

这里先谈初始化:


struct gongji
{
    int spell[11],output,hp;//spell数组用来标记这种攻击方式是否已经用过了.output自然是输出.hp表示当前敌人还有的血量.
}now,nex;
int a[11][2];//a[][0]存单倍伤害值a[][1]存双倍伤害条件

接下来就是bfs的主体了~

int bfs()
{
    for(int i=0;i<11;i++)
    {
        now.spell[i]=0;
    }//初始化第一个push进去的数组~标记上所有攻击方式都没用过.
    now.output=0;
    now.hp=m;
    queue<gongji>s;
    s.push(now);
    int caonima=0;
    while(!s.empty())
    {
        now=s.front();
        s.pop();
        for(int i=0;i<n;i++)//这里的操作还是很容易理解的.
        {
            if(now.spell[i]==1)continue;
            nex=now;
            nex.spell[i]=1;
            nex.output=now.output+1;
            if(now.hp<=a[i][1])nex.hp=now.hp-2*a[i][0];
            else  nex.hp=now.hp-a[i][0];
            if(nex.hp<=0)return nex.output;
            s.push(nex);
        }
    }
    return -1;
}


最后贴上AC完整代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int n,m;
struct gongji
{
    int spell[11],output,hp;
}now,nex;
int a[11][2];
int bfs()
{
    for(int i=0;i<11;i++)
    {
        now.spell[i]=0;
    }
    now.output=0;
    now.hp=m;
    queue<gongji>s;
    s.push(now);
    int caonima=0;
    while(!s.empty())
    {
        now=s.front();
        s.pop();
        for(int i=0;i<n;i++)
        {
            if(now.spell[i]==1)continue;
            nex=now;
            nex.spell[i]=1;
            nex.output=now.output+1;
            if(now.hp<=a[i][1])nex.hp=now.hp-2*a[i][0];
            else  nex.hp=now.hp-a[i][0];
            if(nex.hp<=0)return nex.output;
            s.push(nex);
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++)
        {
           scanf("%d%d",&a[i][0],&a[i][1]);
        }
        printf("%d\n",bfs());
    }
}













你可能感兴趣的:(杭电,bfs,杭电2616)