首页 > ACM题库 > HDU-杭电 > HDU 1044 Collect More Jewels-BFS-[解题报告] C++
2013
11-26

HDU 1044 Collect More Jewels-BFS-[解题报告] C++

Collect More Jewels

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

3

4 4 2 2
100 200
****
[email protected]*
*B<*
****

4 4 1 2
100 200
****
[email protected]*
*B<*
****

12 5 13 2
100 200
************
*B.........*
*.********.*
*@...A....<*
************

样例输出:

Case 1:
The best score is 200.

Case 2:
Impossible

Case 3:
The best score is 300.

分别用dfs, bfs做了一次

dfs超时,bfs超内存

题目意思  求解是否能够到达出口 如果能 求解到达时的最大携带价值
首先使用广搜搜出包括起点和终点在内 所有特殊点之间的最短距离 建立一个隐式图
然后使用DFS枚举各个组合 然后求出最大值 注意DFS的强剪

代码思想很好,但是代码风格让人看起来难受,改了半天还是这个样子,下次重写。

#include <iostream>
#include <queue>
#include <cmath>
using namespace std;

long value[20];
long Step[20][20];
char Map[60][60];
long W,H,L,M;
int stx, sty;

typedef struct
{
	long mi,mj;
	long step;
	long from;
}Node;

queue<Node> q;

bool hs[60][60][20];
long dx[]={0,1,-1,0};
long dy[]={1,0,0,-1};


long b;

inline void BFS()
{
	while (!q.empty())
	{
		q.pop();
	}
	memset(hs,0,sizeof(hs));

	Node point;
	point.mi = stx;
	point.mj = sty;
	point.from = 0;
	point.step = 0;
	q.push(point);
	hs[point.mi][point.mj][point.from]=true;


	while (!q.empty())
	{
		Node cur=q.front();
		q.pop();

		long j;
		Node next;

		for (j=0;j<4;++j)
		{
			next.mi=cur.mi+dx[j];
			next.mj=cur.mj+dy[j];
			next.step=cur.step+1;
			next.from=cur.from;
			if (next.mi>=0&&next.mi<H&&next.mj>=0&&next.mj<W)
			{
				if (next.step<=L&&!hs[next.mi][next.mj][next.from]&&Map[next.mi][next.mj]!='*')
				{
					hs[next.mi][next.mj][next.from]=true;
					q.push(next);

					if (Map[next.mi][next.mj]>='A'&&Map[next.mi][next.mj]<='J')
					{
						Step[next.from][Map[next.mi][next.mj]-'A'+2]=Step[Map[next.mi][next.mj]-'A'+2][next.from]=next.step;
						next.from=Map[next.mi][next.mj]-'A'+2;
						next.step=0;
						hs[next.mi][next.mj][next.from]=true;
						q.push(next);    
					}                
					else if (Map[next.mi][next.mj]=='@')
					{
						Step[next.from][0]=Step[0][next.from]=next.step;
						next.from=0;
						next.step=0;
						hs[next.mi][next.mj][next.from]=true;
						q.push(next);    
					}
					else if (Map[next.mi][next.mj]=='<')
					{
						Step[next.from][1]=Step[1][next.from]=next.step;
						next.from=1;
						next.step=0;
						hs[next.mi][next.mj][next.from]=true;
						q.push(next);    
					}
				}
			}

		}
	}
}

bool vist[20];
long lmax;
long all;

inline void dfs(long pos,long now,long step)
{
	if (lmax==all||step>L)
	{
		return;
	}
	if (Step[pos][1]!=-1&&step+Step[pos][1]<=L&&now>lmax)
	{
		lmax=now;
	}
	long i;
	for (i=2;i<M+2;++i)
	{
		if (!vist[i]&&Step[pos][i]!=-1)
		{
			vist[i]=true;
			dfs(i,now+value[i],step+Step[pos][i]);
			vist[i]=false;
		}
	}
}


int main()
{
	long T;
	scanf("%ld",&T);
	b=1;
	while (T--)
	{
		scanf("%ld%ld%ld%ld",&W,&H,&L,&M);
		long i,j;
		all=0;
		for (i=2;i<M+2;++i)
		{
			scanf("%ld",&value[i]);
			all+=value[i];
		}
		value[0]=value[1]=0;

		gets(Map[0]);
		for (i=0;i<H;++i)
		{
			gets(Map[i]);
			for (j=0;j<W;++j)
			{
				if (Map[i][j]=='@')
				{
					stx = i;
					sty = j;
				}
			}
		}

		memset(Step,-1,sizeof(Step));
		BFS();
		if (b!=1)
			puts("");
		printf("Case %ld:\n",b++);

		if (Step[0][1]==-1||Step[0][1]>L)
		{
			puts("Impossible");
			continue;
		}
		memset(vist,0,sizeof(vist));
		lmax=-1;
		vist[0]=true;
		dfs(0,0,0);
		if (lmax!=-1)
			printf("The best score is %ld.\n",lmax);
		else
			puts("Impossible");                
	}
	return 0;
}


, ,