Kill the monster

本文介绍了一道名为“杀怪兽”的算法题,题目要求玩家使用不同的魔法攻击来击败怪物,并探讨了两种不同的解决方案。一种是通过深度优先搜索算法进行求解,另一种则是利用全排列算法寻找最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Kill the monster

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


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
 

Author

#include<iostream>
#include<cstring>
using namespace std;
int a[15],b[15];
int used[15];
int minf;
int n,m;
void DFS(int blood,int cnt)
{
 if(cnt>n)
  return ;
 if(cnt<minf&&blood<=0)
 {
  minf=cnt;
  return ;
 }
 
 for(int i=1;i<=n;i++)
 {
  if(!used[i])
  {
   used[i]=1;
   if(blood<=b[i])
    DFS(blood-2*a[i],cnt+1);
   if(blood>b[i])
    DFS(blood-a[i],cnt+1);
   used[i]=0;
  }
 }
}

int main()
{
 while(cin>>n>>m)
 {
  for(int i=1;i<=n;i++)
  {
   cin>>a[i]>>b[i];
  }
  memset(used,0,sizeof(used));
  minf=20;
  
  DFS(m,0);
  if(minf!=20)
   printf("%d\n",minf);
  else
   printf("-1\n");
  
 }
 return 0;
 
}

方法二:

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
    int o[10],a[10],m[10];
    int n,M,count,min;

    while(cin>>n>>M)
    {
        for(int i=0;i<n;i++)
        cin>>a[i]>>m[i];
        for(int i=0;i<n;i++)
        o[i] = i;
        min = 100;
        do{
            count = 0;
            int tem = M;
            for(int i=0;i<n;i++)
            {
                count++;
                if(tem<=m[o[i]])
                tem -= a[o[i]]*2;
                else tem -= a[o[i]];
                if(tem<=0)
                {
                    if(count<min)
                    min = count;
                    break;
                }
            }
        }while(next_permutation(o,o+n));
        if(min==100)printf("-1\n");
        else printf("%d\n",min);
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值