题目:
Kill the monster |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 133 Accepted Submission(s): 97 |
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 yifenfei |
Source 奋斗的年代 |
Recommend yifenfei |
题目分析:
深搜。简单题。其实这道题基本没什么难度。只要把题目看懂。
代码如下:
/*
* h.cpp
*
* Created on: 2015年2月26日
* Author: Administrator
*/
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 11;
struct Node{//技能的结构体
int spell;//伤害值
int M;//如果放出这个大招的时候,怪物的hp小雨这个值,那么这时候的伤害值是spell*2
}node[maxn];
bool visited[maxn];//用于标记某一个技能是否已经使用过
int ans;//杀死怪物所需要的最小技能数
int n;//总共的技能数
int hp;//怪物的能量值
/**
* 深搜.
* k:表示目前放到了第几个技能
* hp:目前怪物的能量值
*/
void dfs(int k,int hp){
/**
* 越界判断
*/
if(k >= 11){//如果已经把所有的技能都已经放完了
return ;//则返回.
}
/**
* 判断是否成功
*/
if(hp <= 0){//如果怪物的能量值已经<=0
if(ans > k){//如果当前保存的最小技能数<当前的最小技能数
ans = k;//更新一下包村的最小技能数
}
return ;//返回
}
int i;
for(i = 1 ; i <= n ; ++i){//遍历所有的技能
if(visited[i] == false){//如果该技能还没有被使用
visited[i] = true;//将该技能标记为已经使用
if(hp <= node[i].M){//如果怪物的能量值<=m
dfs(k+1,hp-node[i].spell*2);//那么这时候造成的伤害值是spell*2.继续使用下一个技能
}else{//否则
dfs(k+1,hp-node[i].spell);//这时候造成的上海市spell
}
visited[i] = false;//回滚.重新将该技能标记为没有被访问
}
}
}
int main(){
while(scanf("%d%d",&n,&hp)!=EOF){
int i;
for(i = 1 ; i <= n ; ++i){
scanf("%d%d",&node[i].spell,&node[i].M);
}
memset(visited,false,sizeof(visited));
ans = 11;//初始化技能的最小使用次数
dfs(0,hp);
if(ans == 11){//如果ans一直没有被更新
printf("-1\n");//那么证明怪物没有被杀死
}else{//否则,则证明怪物被杀死了
printf("%d\n",ans);//输出使用技能的最小次数
}
}
return 0;
}