HDU T2616 Kill the monster

版权声明:希望能帮到弱校的ACMer成长,因为自己是弱校菜鸡~~~~ https://blog.csdn.net/Mr__Charles/article/details/82110410

                    HDU T2616 Kill the monster 


题目思路:

    因为隐含每个技能只能用一次,所以用Dfs比较快,但鄙人还是给出两种写法......

 

Dfs写法

#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int n,m,scnt;
int s[10][2];
bool vis[10];

void Dfs(int hp,int cnt){
	if(cnt >= 10) return ;
	if(hp <= 0){
	    scnt = min(scnt,cnt);
	    return ;
	}
	
	for(int i = 0; i < n; ++i){
		if(!vis[i]){
			vis[i] = true;
			if(hp <= s[i][1])
			    Dfs(hp - s[i][0] * 2,cnt+1);
			else 
			    Dfs(hp - s[i][0],cnt+1);
			vis[i] = false;
		}
	}
}

int main(){
	while(~scanf("%d%d",&n,&m)){
		for(int i = 0; i < n ; ++i){
		    scanf("%d%d",&s[i][0],&s[i][1]);
		    vis[i] = false;
		}
	scnt = INF;
	Dfs(m,0);
	if(scnt >= 10)
	    puts("-1");
	else
	printf("%d\n",scnt);
	}
	return 0 ;
}

Bfs写法

#include<cstdio>
#include<iostream>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
int n,m,s[10][2];

struct spell{
	int scnt,hp;
	bool vis[10];
}now,nex;

int Bfs(){
	for(int i = 0; i < n; i++)
	    now.vis[i] = false;
	now.scnt = 0;
	now.hp = m; 
	queue<spell> Q;
	Q.push(now);
	while(!Q.empty()){
		now = Q.front();
		Q.pop();
		for(int i = 0; i < n ;i++){
			if(!now.vis[i]){
				nex = now;
				nex.scnt++;
				nex.vis[i] = true;
				if(nex.hp <= s[i][1])
				    nex.hp -= s[i][0] * 2;
				else 
				    nex.hp -= s[i][0];
				    
				if(nex.hp <= 0) 
				    return nex.scnt;
				    
				if(nex.scnt >= 10)
				    return -1;
				    
				Q.push(nex);
			}
		}
	}
	return -1;
}

int main(){
	while(~scanf("%d%d",&n,&m)){
		for(int i = 0; i < n ;i++)
		    scanf("%d%d",&s[i][0],&s[i][1]);
		printf("%d\n",Bfs());
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Mr__Charles/article/details/82110410