hdu1044 Collect More Jewels----BFS+DFS

原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=1044


一:原题内容

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.
 
二:分析理解

题目大意:

在一个迷宫中,从起点走到终点,还有几个宝物,问在给定的时间内,到达终点后所能获取的最大价值。

思路:

先用bfs求出入口,宝物,出口,两两之间的最短距离。

在用dfs搜索所有情况,求出从入口走到出口能获得的最大价值。

熟悉两种搜索的优缺点:

BFS: 对于解决最短或最少问题特别有效,而且寻找深度小,但缺点是内存耗费量大(需要开大量的数组单元用来存储状态)。

DFS:对于解决遍历和求所有问题有效,对于问题搜索深度小的时候处理速度迅速,然而在深度很大的情况下效率不高

 

这个程序效率还没有很高,好像有的可以1次bfs就把入口、宝物、出口两两的距离找出来的。继续改进吧。


三:AC代码

#include<iostream>  
#include<algorithm>  
#include<queue>

using namespace std;

int w, h, l, m;//h行,w列,l时间,m宝石数量
int val[60];//1到m个宝石的价值
char matrix[60][60];
bool used[60][60];//bfs访问标记
bool visited[60];//dfs访问标记
int step[60][60];//走到这步话花费的时间
int ans;//结果
int sum;//所有宝石的价值和
int mov[4][2] = { { -1,0 },{ 1,0 },{ 0,1 },{ 0,-1 } };
int dis[60][60];//记录初始位置、各宝石和出口两两间的距离

				/* 从(x1,y1)到其他点的距离,s范围是0到m+1,0代表起始位置,m+1表示出口,1到m分别表示第i个宝物 */
void BFS(int x1, int y1, int s)
{
	queue<int> q;
	memset(used, 0, sizeof(used));
	memset(step, 0, sizeof(step));

	int u = x1*w + y1;
	q.push(u);
	used[x1][y1] = true;
	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 + mov[i][0];
			int yy = y + mov[i][1];
			if (xx < 0 || xx >= h || yy < 0 || yy >= w) continue;
			if (used[xx][yy] || matrix[xx][yy] == '*') continue;
			used[xx][yy] = true;
			step[xx][yy] = step[x][y] + 1;
			if (matrix[xx][yy] == '@') dis[s][0] = dis[xx][yy];
			else if (matrix[xx][yy] == '<') dis[s][m + 1] = step[xx][yy];
			else if (matrix[xx][yy] >= 'A'&&matrix[xx][yy] <= 'J')
				dis[s][matrix[xx][yy] - 'A' + 1] = step[xx][yy];

			q.push(xx*w + yy);
		}
	}
}

/* s表示当前点,value表示获得的价值,time表示花费的时间 */
void DFS(int s, int value, int time)
{
	if (time > l) return;
	if (ans == sum) return;
	if (s > m)
	{
		if (value > ans)
			ans = value;
		return;
	}
	for (int i = 0; i <= m + 1; i++)
	{
		if (dis[s][i] == 0 || visited[i]) continue;
		visited[i] = true;
		DFS(i, value + val[i], time + dis[s][i]);
		visited[i] = false;
	}
}

int main()
{
	int T;
	scanf("%d", &T);
	for (int iCase = 1; iCase <= T; iCase++)
	{
		memset(dis, 0, sizeof(dis));
		scanf("%d%d%d%d", &w, &h, &l, &m);
		sum = 0;
		ans = -1;

		for (int i = 1; i <= m; i++)
		{
			scanf("%d", &val[i]);
			sum += val[i];
		}

		val[0] = val[m + 1] = 0;//!!!!

		for (int i = 0; i < h; i++)
			scanf("%s", &matrix[i]);

		for (int i = 0; i < h; i++)
		{
			for (int j = 0; j < w; j++)
			{
				if (matrix[i][j] == '@') BFS(i, j, 0);
				else if (matrix[i][j] == '<') BFS(i, j, m + 1);
				else if (matrix[i][j] >= 'A'&&matrix[i][j] <= 'J')
					BFS(i, j, matrix[i][j] - 'A' + 1);
			}
		}
		memset(visited, 0, sizeof(visited));
		visited[0] = true;

		DFS(0, 0, 0);

		printf("Case %d:\n", iCase);
		if (ans >= 0)printf("The best score is %d.\n", ans);
		else printf("Impossible\n");

		if (iCase!=T)
			printf("\n");
	}

	return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值