首页 > 学院 > 开发设计 > 正文

hdu1044【bfs+dfs】

2019-11-10 19:17:29
字体:
来源:转载
供稿:网友

Collect More Jewels

Time Limit: 2000/1000 MS (java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7334 Accepted Submission(s): 1705

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.

一个迷宫,L时间后倒塌,里面有M个宝藏。问能否逃出,如果逃出,得到的最大价值是多少? 宝藏由大写字母【A】~【J】表示,‘*’表示墙,‘.’表示路,‘@’表示起点,‘<’表示出口。 题解: 宝藏最多十个,可以bfs找出宝藏,起点,出口相互之间的最短距离,然后dfs搜索最大能获得的价值。 剪枝:ans==Max,已经搜到获得最大价值 代码:

#include <iostream>#include <string>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <map>#define MST(s,q) memset(s,q,sizeof(s))#define INF 0x3f3f3f3f#define MAXN 9999using namespace std;struct Node{ int x, y; int sum;} s, u, v;int W, H, L, M, ans, Max; // Max保存所有宝藏价值的和char Map[100][100];int move_x[4] = {1, -1, 0, 0}, move_y[4] = {0, 0, 1, -1};int value[11];int path[20][20]; // 保存(起点,终点,各宝藏 ) 它们直接的最短距离,最多bfs地图十几次bool vis[100][100];bool visited[20];// 求kind到其他点的距离void bfs(int x, int y, int kind) // kind==0 表示起点,kind==M+1表示终点,1-M表示宝藏{ queue<Node> Q; s.x = x, s.y = y, s.sum = 0; MST(vis, 0); Q.push(s); vis[x][y] = 1; while (!Q.empty()) { u = Q.front(); Q.pop(); for (int i = 0; i < 4; i++) { v.x = u.x + move_x[i], v.y = u.y + move_y[i]; if (v.x >= 0 && v.x < H && v.y >= 0 && v.y < W && !vis[v.x][v.y] && Map[v.x][v.y] != '*') { vis[v.x][v.y] = 1; v.sum = u.sum + 1; if (Map[v.x][v.y] != '.') { if (Map[v.x][v.y] == '@') path[kind][0] = v.sum; else if (Map[v.x][v.y] == '<') path[kind][M + 1] = v.sum; else path[kind][Map[v.x][v.y] - 'A' + 1] = v.sum; } Q.push(v); } } }}// kind表示当前节点,time表花费的时间,V表价值void dfs(int kind , int time, int V) // kind==0 表示起点,kind==M+1表示终点,其他表示宝藏{ if (time > L || ans == Max) return; // 超出时间或已经得到最大价值 if (kind == M + 1) { ans = max(ans, V); return; } for (int i = 1; i <= M + 1; i++) { if (!visited[i]) { visited[i] = 1; dfs(i, time + path[kind][i], V + value[i - 1]); visited[i] = 0; } }}int main(){ int T, sx, sy, icase = 1; cin >> T; while (T--) { cin >> W >> H >> L >> M; Max = 0; for (int i = 0; i < M; i++) { scanf("%d", &value[i]); Max += value[i]; } value[M] = 0; for (int i = 0; i < H; i++) scanf("%s", Map[i]); MST(path, INF); // 把距离初始为最大 for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) { if (Map[i][j] == '@') bfs(i, j, 0); else if (Map[i][j] == '<')bfs(i, j, M + 1); else if (Map[i][j] >= 'A' && Map[i][j] <= 'J') bfs(i, j, Map[i][j] - 'A' + 1); } MST(visited, 0); visited[0] = 1; ans = -1; dfs(0, 0, 0); printf("Case %d:/n", icase++); if (ans != -1) printf("The best score is %d./n", ans); else printf("Impossible/n"); if (T) printf("/n"); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表