Collect more jewels
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3548 accepted submission (s): 704
Problem descriptionit 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.
Inputstandard 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.
Outputresults shocould 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 shoshould be separated by a single blank line. no blank line shocould 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
34 4 2 2100 200*****@A**B<*****4 4 1 2100 200*****@A**B<*****12 5 13 2100 200*************B.........**.********.**@...A....<*************
Sample output
Case 1:The best score is 200.Case 2:ImpossibleCase 3:The best score is 300.
Sourceasia 2005, Hangzhou (Mainland China), Preliminary
Recommendjgshining
Solution: Map compression is required for this question. If you search directly (BFS + DFS), the idea will be messy and time-out.
Looking at the question, the first feeling is that BFS + DFS, that is to say, BFs calculates the distance from all the places that can be taken to the end, and then the DFS full graph, decisively timed out. Compress the map, directly use the BFS full graph to find the distance between each gem, start point, and end point, and then use the array storage to form a new map. Then use DFS to search for a new map to get the answer (related basic question (Link): hdu2614 beat ).
There are two main problems:
(1) Positioning and pricing of gems. This problem cannot be solved, leading to the failure of BFS and the formation of new maps. Directly store the data using struct arrays (X, Y, Val). When using the data, you can directly record the subscript to find the map position and value of the gemstone (you need to process the starting and ending points, put them in the first and last sub-objects of the array respectively (relative to W )).
(2) If the DFS search order problem cannot be solved, the answer will be incorrect. If you cannot understand this question in the code, please go to the basic question link and do not analyze it here.
Pruning: you only need to cut out the branches that cannot reach the end point and that have already been traversed (inmap (next. X, next. Y )&&! Flag [next. x] [next. Y] & map [next. x] [next. Y]! = '*'; In DFS: The Gem has not been retained (will not be retained), the time can also go to the end (will not time out), the gem has been marked and the main function, if the start point cannot reach the end point, output impossible directly)
# Include <cstdio> # include <cstring> # include <queue> # include <algorithm> using namespace STD; int N, M, L, W; char map [50] [50]; int flag [50] [50]; int time1 [14] [14]; int flag_jews [14]; int dir [4] [2] = {,-, 0,-1}; // four directions that can be taken, for bfsint F, max_valu; struct node // used for BFS {int X; int y; int t ;}; struct node2 // store the gem information for DFS {int X; int y; int val ;} jews [12]; int inmap (int x, int y) // determines whether the vertex (X, Y) is in the map, used for BFS {If (x> = 0 & x <n & Y> = 0 & Y <m) return 1; return 0;} int BFS (int x, int y, int X1, int Y1) // calculates the vertex (x, y) {int I; node first, next; queue <node> q; If (x = x1 & Y = Y1) return 0; memset (flag, 0, sizeof (FLAG); first. X = x; first. y = y; first. T = 0; flag [x] [Y] = 1; q. push (first); While (! Q. empty () {First = Q. front (); q. pop (); for (I = 0; I <4; I ++) {next. X = first. X + dir [I] [0]; next. y = first. Y + dir [I] [1]; next. T = first. t + 1; if (inmap (next. x, next. Y )&&! Flag [next. x] [next. Y] & map [next. x] [next. Y]! = '*') {If (next. X = x1 & next. y = Y1) return next. t; flag [next. x] [next. y] = 1; q. push (next) ;}}return 10000000;} void DFS (int r, int lD, int W1, int Val) // The R gem. The LD time is left, there are still W gems that haven't been picked up, and the value of the existing gems {If (W = 0) // return; For (INT I = 1; I <W + 1; I ++) {If (! Flag_jews [I] & time1 [I] [W + 1] <= ld-time1 [r] [I]) // The Gem has never been picked up (it won't be replayed ), the time can also reach the end (no timeout) {Val + = Jews [I]. val; flag_jews [I] = 1; max_valu = Val> max_valu? VAL: max_valu; DFS (I, ld-time1 [r] [I], w1-1, Val); Val-= Jews [I]. val; flag_jews [I] = 0 ;}} int main () {int t; int I, j, k; scanf ("% d", & T ); for (k = 1; k <= T; k ++) {f = 0; max_valu = 0; scanf ("% d", & M, & N, & L, & W); Jews [0]. val = 0; Jews [W + 1]. val = 0; for (I = 1; I <= W; I ++) scanf ("% d", & Jews [I]. val); for (I = 0; I <n; I ++) // map input processing {scanf ("% s", map [I]); for (j = 0; j <m; j ++) {If (Map [I] [J]> = 'A' & map [I] [J] <= 'J ') // The Gem {Jews [map [I] [J]- 'A' + 1]. X = I; Jews [map [I] [J]-'A' + 1]. y = J;} else if (Map [I] [J] = '@') // start point {Jews [0]. X = I; Jews [0]. y = J; Map [I] [J] = '. ';} else if (Map [I] [J] =' <') // end {Jews [W + 1]. X = I; Jews [W + 1]. y = J; Map [I] [J] = '. ';}}for (I = 0; I <W + 2; I ++) // the distance between each gemstone heap, starting point, and ending point for (j = 0; j <W + 2; j ++) time1 [I] [J] = time1 [J] [I] = BFS (Jews [I]. x, Jews [I]. y, Jews [J]. x, Jews [J]. y); If (K! = 1) printf ("\ n"); printf ("case % d: \ n", k); If (time1 [0] [W + 1]> L) printf ("impossible \ n"); // the start point goes directly to the end point. The time-out else {memset (flag_jews, 0, sizeof (flag_jews); flag_jews [0] = 1; DFS (0, L, W, 0); printf ("the best score is % d. \ n ", max_valu) ;}} return 0 ;}