Collect More Jewels

Collect More Jewels

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


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.
题的大意是这样的,在一个密室室里,有很多珠宝,每个珠宝的价值不一样,然后里密室坍塌还有t时间,问你一个人从入口到出口用这t时间能拿到的最大价值珠宝是多少。
DFS+BFS
先用bfs求出出口,入口,和各个珠宝放置点两两之间的最小距离,形成一张图(由两两之间距离和每个点的价值(入口和出口的价值为0)组成)再用dfs找出新图的最小值即可。
#include <stdio.h>
#include <ctype.h>
#include <queue>
#include <string.h>
using namespace std;
const int MAX = 60;
int w, h, L, m, t;
int sx, sy, dx, dy;
int p[20];
int map[MAX][MAX];
int dist[MAX][MAX];
int vis[MAX][MAX];
int vis_t[MAX];
int ans;
int sum;
const int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
typedef struct{
	int x;
	int y;
	int step;
}place;
queue<place>q;
void bfs(int x, int y, int r){//广搜生成新图,生成的新图只有m + 2个点,起点,终点加上m个珠宝的点
	while(q.empty() == false)//新图有两部分,一部份是dist数组中的各点之间的距离,另一部分是p数组中的各点的价值
		q.pop();
	place s;
	s.x = x;
	s.y = y;
	s.step = 0;
	vis[x][y] = 1;
	q.push(s);
	while(q.empty() == false)
	{
		s = q.front();
		q.pop();
		for(int i = 0; i < 4; ++i)
		{
			place t;
			t.x = s.x + dir[i][0];
			t.y = s.y + dir[i][1];
			if(t.x < 0 || t.x >= h || t.y < 0 || t.y >= w || vis[t.x][t.y] == 1 || map[t.x][t.y] == -1)
			{
				continue;
			}
			t.step = s.step + 1;
			if(t.x == sx && t.y == sy)
			{
				dist[r][0] = t.step;
			}
			else if(map[t.x][t.y] > 0)
			{
				dist[r][map[t.x][t.y]] = t.step;
			}
			else if(t.x == dx && t.y == dy)
			{
				dist[r][m + 1] = t.step;
			}
			vis[t.x][t.y] = 1;
			q.push(t);
		}
	}
}
void dfs(int pos, int s, int tt){//pos表示广搜后新生成的图的哪个点,s表示到p点拿到了多少珠宝,
	if(tt > t || ans == sum) //t表示时间用了多少
		return;     //不能在规定时间到达出口   
	if(pos > m && s > ans)//到达出口
		ans = s;
	for(int i = 0; i <= m + 1; ++i)
	{
		if(dist[pos][i] == 0 || vis_t[i])
		{
			continue;
		}
	//	printf("%d ", i);
		vis_t[i] = 1;
		dfs(i, s + p[i],  tt + dist[pos][i]);
		vis_t[i] = 0;
	}
}
int main(void){
	int a, k = 0;
	char s[500];
	scanf("%d", &a);
	getchar();
	while(a--)
	{
		memset(map, 0, sizeof(map));
		memset(dist, 0, sizeof(dist));
		memset(vis, 0, sizeof(vis));
		memset(vis_t, 0, sizeof(vis_t));
		memset(p, 0, sizeof(p));
		scanf("%d %d %d %d", &w, &h, &t, &m);
		getchar();
		ans = -1;
		sum = 0;
		for(int i = 1; i <= m; ++i)
		{ 
			scanf("%d", &p[i]);
			sum += p[i];
		}
		getchar();
		for(int i = 0; i < h; ++i)
		{
			scanf("%s", s);
			for(int j = 0; j < w; ++j)
			{
				if(s[j] == '.')
				{
					map[i][j] = 0; //0表示能通过,-1表示墙
				}
				else if(s[j] == '*')
				{
					map[i][j] = -1;
				}
				else if(isalpha(s[j]))
				{
					map[i][j] = s[j] - 'A' + 1;
				}
				else if(s[j] == '<')
				{
					dx = i;
					dy = j;
					map[i][j] = 0;
				}
				else if(s[j] == '@')
				{
					sx = i;
					sy = j;
					map[i][j] = 0;
				}
			}
		}
		for(int i = 0; i < h; ++i)
		{
			for(int j = 0; j < w; ++j)
			{
				memset(vis, 0, sizeof(vis));
				if(i == sx && j == sy)
				{
					bfs(i, j, 0);
				}
				else if(map[i][j] > 0)
				{
					bfs(i, j, map[i][j]);
				}
				else if(i == dx && j == dy)
				{
					bfs(i, j, m + 1);
				}
			}
		}
		vis_t[0] = 1;
		dfs(0, 0, 0);
		printf("Case %d:\n", ++k);
		if(ans >= 0)
		{
			printf("The best score is %d.\n", ans);
		}
		else
		{	
			printf("Impossible\n");
		}
		if(a)
			printf("\n");
		gets(s);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值