HDU 1044 Collect More Jewels (BFS+DFS)

14 篇文章 0 订阅
1 篇文章 0 订阅

Collect More Jewels

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8917    Accepted Submission(s): 2096


 

Problem Description

It is written in the Book of The Lady: After the Creation, the cruel god Moloch rebelled against the authority of Marduk the Creator.Moloch stole from Marduk the most powerful of all the artifacts of the gods, the Amulet of Yendor, and he hid it in the dark cavities of Gehennom, the Under World, where he now lurks, and bides his time.

Your goddess The Lady seeks to possess the Amulet, and with it to gain deserved ascendance over the other gods.

You, a newly trained Rambler, have been heralded from birth as the instrument of The Lady. You are destined to recover the Amulet for your deity, or die in the attempt. Your hour of destiny has come. For the sake of us all: Go bravely with The Lady!

If you have ever played the computer game NETHACK, you must be familiar with the quotes above. If you have never heard of it, do not worry. You will learn it (and love it) soon.

In this problem, you, the adventurer, are in a dangerous dungeon. You are informed that the dungeon is going to collapse. You must find the exit stairs within given time. However, you do not want to leave the dungeon empty handed. There are lots of rare jewels in the dungeon. Try collecting some of them before you leave. Some of the jewels are cheaper and some are more expensive. So you will try your best to maximize your collection, more importantly, leave the dungeon in time.

 

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 10) which is the number of test cases. T test cases follow, each preceded by a single blank line.

The first line of each test case contains four integers W (1 <= W <= 50), H (1 <= H <= 50), L (1 <= L <= 1,000,000) and M (1 <= M <= 10). The dungeon is a rectangle area W block wide and H block high. L is the time limit, by which you need to reach the exit. You can move to one of the adjacent blocks up, down, left and right in each time unit, as long as the target block is inside the dungeon and is not a wall. Time starts at 1 when the game begins. M is the number of jewels in the dungeon. Jewels will be collected once the adventurer is in that block. This does not cost extra time.

The next line contains M integers,which are the values of the jewels.

The next H lines will contain W characters each. They represent the dungeon map in the following notation:
> [*] marks a wall, into which you can not move;
> [.] marks an empty space, into which you can move;
> [@] marks the initial position of the adventurer;
> [<] marks the exit stairs;
> [A] - [J] marks the jewels.

Output

Results should be directed to standard output. Start each case with "Case #:" on a single line, where # is the case number starting from 1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.

If the adventurer can make it to the exit stairs in the time limit, print the sentence "The best score is S.", where S is the maximum value of the jewels he can collect along the way; otherwise print the word "Impossible" on a single line.

 

Sample Input

3 4 4 2 2

100 200

****

*@A*

*B<*

****

4 4 1 2

100 200

****

*@A*

*B<*

****

12 5 13 2

100 200

************

*B.........*

*.********.*

*@...A....<*

************

Sample Output

Case 1:

The best score is 200.

Case 2:

Impossible

Case 3:

The best score is 300.

Source

Asia 2005, Hangzhou (Mainland China), Preliminary

 题意:

从起点在规定的时间内到达终点,路上还可以捡点宝藏~,最后求到达终点所能获得的最大价值

先用bfs处理宝物起点终点之间的最短距离,

然后用dfs搜索所有情况。

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#define ll long long
#define inf 0x3f3f3f3f
#define Max 55
using namespace std;
string mapp[Max];
int w,h,l,m;
//l是时间限制 m是珠宝的数量
int val[Max];//宝石的价值 1-M
int used[Max][Max];//bfs时访问标记
int vis[Max];//dfs时访问
int step[Max][Max];
int ans;//结果
int sum;//所有宝石的价值总和
int dir[4][2]= {{-1,0},{1,0},{0,1},{0,-1}};
int dis[Max][Max];//记录初始位置,各宝石和出口两两间的距离
//从(x1,y1) 点到其他点的距离 s(0-m+1)是改点的标号,0表示初始位置
//m+1表示出口,1-m表示第i个宝物队
queue<int >q;
void bfs(int x1,int y1,int s) { //处理最短距离
	while(!q.empty())  q.pop();//必须要
	memset(used,0,sizeof(used));
	memset(step,0,sizeof(step));
	int u=x1*w+y1;//二维转一维
	q.push(u);
	used[x1][y1]=1;
	step[x1][y1]=0;
	while(!q.empty()) {
		u=q.front();
		q.pop();
		int x=u/w;
		int y=u%w;
		for(int i=0; i<4; i++) {
			int xx=x+dir[i][0];
			int yy=y+dir[i][1];
			if(xx<0||xx>=h||yy<0||yy>=w)continue;
			if(used[xx][yy]||mapp[xx][yy]=='*') continue;
			used[xx][yy]=1;
			step[xx][yy]=step[x][y]+1;
			if(mapp[xx][yy]=='@') dis[s][0]=step[xx][yy];
			else if(mapp[xx][yy]=='<') dis[s][m+1]=step[xx][yy];
			else if(mapp[xx][yy]>='A'&&mapp[xx][yy]<='J')
				dis[s][mapp[xx][yy]-'A'+1]=step[xx][yy];
			q.push(xx*w+yy);
		}
	}
}
//dfs ,s表示当前点,value表示获得的价值,time表示花费的时间
void dfs(int s,int value,int time) {
	if(time>l) return ;//剪枝
	if(ans==sum) return ;
	if(s==m+1) {//m+1代表出口 
		if(value>ans) ans=value;
		return ;
	}
	for(int i=0; i<=m+1; i++) {
		if(dis[s][i]==0||vis[i]) continue;
		vis[i]=1;
		dfs(i,value+val[i],time+dis[s][i]);
		vis[i]=0;
	}
}
int main() {
	int t;
	cin>>t;
	for(int Case=1; Case<=t; Case++) {
		memset(dis,0,sizeof(dis));
		cin>>w>>h>>l>>m;
		sum=0;
		ans=-1;
		for(int i=1; i<=m; i++) {
			cin>>val[i];
			sum+=val[i];
		}
		val[0]=val[m+1]=0;
		for(int i=0; i<h; i++)
			cin>>mapp[i];
		for(int i=0; i<h; i++) {
			for(int j=0; j<w; j++) {
				if(mapp[i][j]=='@') bfs(i,j,0);//起点到其他……的距离(时间)
				else if(mapp[i][j]=='<') bfs(i,j,m+1);//终点到其他……的距离(时间) 
				else if(mapp[i][j]>='A'&&mapp[i][j]<='J')
					bfs(i,j,mapp[i][j]-'A'+1);//宝藏到其他……的距离(时间)
			}
		}
		memset(vis,0,sizeof(vis));
		vis[0]=1;
		dfs(0,0,0);//0点是起点
//		cout<<ans<<endl;
		cout<<"Case "<<Case<<":"<<endl;
		if(ans>=0) cout<<"The best score is "<<ans<<"."<<endl;
		else cout<<"Impossible"<<endl;
		if(Case!=t) cout<<endl;
	}
	return 0;
}

还有一种方法是 bfs+状压。等我想补充了我再补充 我要去吃炒鸡了 哈哈哈哈hhhh

……炒鸡关门了,,,,嘤嘤嘤……

状压:A-J 一共十位  可以用 十位 二进制来表示。每个珠宝对应一个独一无二的二进制, 也即一个十进制。那么,就可以用一个数字(二进制或十进制)来表示这个珠宝的有或无, 通常二进制的 1 表示有, 用 0 表示没有。

初始都是 0000000000

搜到A 之后 是 0000000001

接着搜到B之后是 0000000010

0000000001 | 0000000010 = 0000000011  就代表同时拥有 A,B,

二进制真好啊嘿嘿(os:这个世界本来就应该是二进制的世界……)

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#define Max 55
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
string mapp[Max];
int w,h,l,m;
//宽高 时间限制,珠宝数量 
int Val[Max],ans;
int vis[Max][Max][(1<<10)+1];//状压 用 10位二进制表示 珠宝是否被取 
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
struct Node{
	int x,y,step;
	int val;//走到这一步的价值 
	int state;//代表所拥有的珠宝 
	Node(int xx=0,int yy=0,int sstep=0,int vval=0,int sstate=0):x(xx),y(yy),step(sstep),val(vval),state(sstate){} 
}st,cur,nxt; 
queue<Node> q;
int bfs()
{
	while(!q.empty()) q.pop();
	q.push(st);
	memset(vis,0,sizeof(vis));
	vis[st.x][st.y][0]=1;
	while(!q.empty())
	{
		cur=q.front();
		q.pop();
		if(cur.step>l) break;
		if(mapp[cur.x][cur.y]=='<')
		{
			ans=max(ans,cur.val);
		}
		for(int i=0;i<4;i++)
		{
			nxt=cur;
			//cout<<"******"<<cur.x<<","<<cur.y<<endl;
			nxt.step+=1;
			nxt.x+=dir[i][0];
			nxt.y+=dir[i][1];	
			if(nxt.x<0||nxt.y<0||nxt.x>=h||nxt.y>=w||mapp[nxt.x][nxt.y]=='*') continue;
			if(vis[nxt.x][nxt.y][nxt.state]) continue;
			if(mapp[nxt.x][nxt.y]>='A'&&mapp[nxt.x][nxt.y]<='J')
			{
				if((nxt.state &(1<<(mapp[nxt.x][nxt.y]-'A')))==0) //说明没有访问过 
				{
					nxt.state=nxt.state|(1<<(mapp[nxt.x][nxt.y]-'A'));//合并  ->00000000010 | 0001000000 = 00010000010 
					nxt.val+=Val[mapp[nxt.x][nxt.y]-'A'];
					//cout<<"****: "<<nxt.x<<","<<nxt.y<<" ---> "<<nxt.val<<endl;
				}
			} 
			if(!vis[nxt.x][nxt.y][nxt.state])
			{
				q.push(nxt);
				vis[nxt.x][nxt.y][nxt.state]=1;
			}
		}
	}
	return ans;
}
int main()
{
	int t;
	cin>>t;
	for(int Case=1;Case<=t;Case++)
	{
		cin>>w>>h>>l>>m;//宽,高 
		for(int i=0;i<m;i++)
			cin>>Val[i];
//		int sx,sy,ex,ey;
		
		for(int i=0;i<h;i++)
		{
			cin>>mapp[i];
			for(int j=0;j<w;j++)
			{
				if(mapp[i][j]=='@')
				{
					st=Node(i,j,0,0,0);
				}
			}
		}
		ans=-1;
		cout<<"Case "<<Case<<":"<<endl;
		bfs();
//		cout<<ans<<endl;
		if(ans>0)//怎么把=号去掉就给过了呢,,,,omygod 
		{
			cout<<"The best score is "<<ans<<"."<<endl;
		}else 
		{
			cout<<"Impossible"<<endl; 
		}
		if(Case!=t) cout<<endl;
	}
	return 0;
 } 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值