Kill the monster

    Kill the monster (简单的dfs)

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


题目大意:首先背景是 有一个村庄的英雄单撸怪物.这个英雄会n种魔法(每次只能使用一次),怪物有m血量.当m<=0时怪物GG.然后呢,英雄会的这个魔法不是一般的魔法(你懂得啦,普通游戏设定),每种魔法有Ai和Mi Ai 代表这种魔法的伤害,当怪物血量少于Mi时 当前魔法伤害翻2倍.

然后呢 让我们求英雄最少攻击几次能干掉怪物.(注意,每种魔法只能用一次)

思路:很明显是个搜索,具体什么搜索我们看他的要求(最少几次,求最优解),所以用dfs.因为要求最优次数,所以我们要对次数进行比较选择.dfs(次数,血量) 这个两个变量.要注意的是 血量和魔法双倍暴击的关系.如果低于Mi则双倍暴击.

这里就套一下常用模版

dfs()
{
    if条件判断——(到终点) 
    {
        操作……
        return;
    }
    if……省略一些条件判断(剪枝优化,比如一些没必要的continue或者计算过的子问题的标记备忘录直接存取)
    for()
        if……满足条件的筛选
        修改标记;
        dfs();
        改回标记;
}

下面直接上代码:

#include<stdio.h>
#include<string.h>
int n,m,minn,vis[20];
struct attack
{
    int ai,mi;
}spell[20];
void dfs(int r,int hp)
{
    if(hp<=0)//如果怪物GG了
    {
        minn=r<minn?r:minn;//那么判断用的魔法次数,进行比较优选.
        return ;
    }
    else if(r==n)//如果怪物没死,但是魔法用完了,这里要注意返回.
    {
        return ;
    }
    for(int i=0;i<n;i++)
    {
        if(!vis[i])
        {
            vis[i]=1;
            if(hp<=spell[i].mi)//我这里写的比较繁琐,就不优化了.
            {
                hp-=spell[i].ai*2;
                dfs(r+1,hp);//这里我写成dfs(r++,hp-spell[i].ai*2),是我的致命错误.可以说是十分之笨了.
                hp+=spell[i].ai*2;//血量别忘了加回来.不然怪物得被气死???
                vis[i]=0;
            }
            else
            {
                hp-=spell[i].ai;
                dfs(r+1,hp);//这里我写成dfs(r++,hp-spell[i].ai),是我的致命错误.可以说是十分之笨了.
                hp+=spell[i].ai;
                vis[i]=0;
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        minn=12;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
            scanf("%d%d",&spell[i].ai,&spell[i].mi);

        dfs(0,m);

        if(minn!=12)
            printf("%d\n",minn);
        else
            printf("-1\n");
    }
}

ps:2018年3月20日 17:31:46

这题是昨天晚上做的,做的时候一点思路都没有,看了博客上的思路,自己打了一会儿,该抓的点没抓住,可以说是还是什么都不会.

然后看了代码,理解了,也打了,弄了半天却被dfs()里的r++和r+1弄错,可以说自己真的是十分笨.

半个学期过去了,dfs是上个学期的知识,我并没有抓牢,学的并不好.

这学期打算从头来过.虽然已经落了很多东西,但是不管怎样,我都会好好学下去,不会放弃的.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值