4.3.7 Kill the monster

Kill the monster

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 118    Accepted Submission(s): 86

 
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
 
 
      
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;

const int MAXN = 10;
int a[MAXN], m[MAXN];
int n, HP;
struct node{
	int hp, t;
	bool vis[MAXN];
};
int dfs(){
	int mint = 1e6;
	stack<node> s;
	node cur, next;
	memset(cur.vis, 0, sizeof(cur.vis));
	cur.hp = HP;
	cur.t = 0;
	while(!s.empty()) s.pop();
	s.push(cur);
	while(!s.empty()){
		cur = s.top();
		s.pop();
		for(int i = 0; i < n; i++){
			next = cur;
			if(!next.vis[i]){				
				if(next.hp <= m[i]){
					next.hp -= 2 * a[i];
				}
				else next.hp -= a[i];
				next.t++;
				next.vis[i] = 1;
				if(next.hp <= 0){
					if(mint > next.t) mint = next.t;
					continue;
				} 
				s.push(next);
			}
			
			
		}
	}
	if(mint == 1e6)
		return -1;
	else return mint;
}
int main(){
	while(scanf("%d%d", &n, &HP) != EOF){
		for(int i = 0; i < n; i++){
			scanf("%d%d", &a[i], &m[i]);
		}
		
		int ans = dfs();
		printf("%d\n", ans);
	}
	return 1;
}

猜你喜欢

转载自blog.csdn.net/bruce_teng2011/article/details/38520177
今日推荐