Collect more jewels
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3545 accepted submission (s): 703
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: walk through the maze, tell you the start and end, there are several treasures, ask you in the given L time, what is the maximum jewelry value after reaching the end. PS: There are two solutions to this problem: BFS + state compression, and BFS + DFS.
Idea (1 ): BFS + state compression 921 Ms dangerous over vis [x] [y] [State] judge the state of Heavy jewelry to all States because up to 10 kinds of jewelry can be used 0 ~ (1 <10) less than the number indicates the status of the jewelry to Be Taken. Remember that the same jewelry cannot be taken twice! I am stuck here.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <queue> # define maxn 51 using namespace STD; int N, M, L, CNT, ans; int Sx, Sy, ex, ey; int A [15]; int DX [] = {-,}; int dy [] = }; bool vis [maxn] [maxn] [1 <10]; char s [maxn]; char MP [maxn] [maxn]; struct node {int X, Y, step, val; int state;} cur, now; queue <node> q; bool BFS () {int I, j, NX, NY, TX, Ty, nstep, nval, NST, TST, TMP; int head = 0, tail =-1; memset (VI S, 0, sizeof (VIS); While (! Q. empty () Q. pop (); ans =-1; cur. X = SX; cur. y = sy; cur. step = 0; cur. val = 0; cur. state = 0; q. push (cur); vis [SX] [sy] [0] = 1; while (! Q. empty () {now = Q. front (); q. pop (); Nx = now. x; ny = now. y; nstep = now. step; nval = now. val; NST = now. state; If (nstep> L) break; // note the exit condition if (Nx = ex & ny = ey) // update ans {If (ANS <nval) ans = nval ;}for (I = 0; I <4; I ++) {TST = NST; Tx = NX + dx [I]; ty = ny + dy [I]; cur. val = nval; If (MP [TX] [ty]> = 'A' & MP [TX] [ty] <= 'J ') {TMP = MP [TX] [ty]-'A'; If (! (TST & (1 <TMP) cur. val + = A [TMP]; // The same jewelry cannot be obtained twice TST = TST | (1 <TMP );} if (TX> = 1 & TX <= N & ty> = 1 & ty <= M & MP [TX] [ty]! = '*'&&! Vis [TX] [ty] [TST]) {vis [TX] [ty] [TST] = 1; cur. X = TX; cur. y = ty; cur. step = nstep + 1; cur. state = TST; q. push (cur) ;}}if (ANS =-1) return false; return true;} int main () {int I, j, T, T1 = 0; scanf ("% d", & T); While (t --) {T1 ++; scanf ("% d", & M, & N, & L, & CNT); for (I = 0; I <CNT; I ++) {scanf ("% d", & A [I]);} for (I = 1; I <= N; I ++) {scanf ("% s", S); For (j = 1; j <= m; j ++) {MP [I] [J] = s [J-1]; If (s [J-1] = '@') {SX = I; Sy = J ;} if (s [J-1] = '<') {EX = I; ey = J ;}} if (T1> 1) printf ("\ n "); printf ("case % d: \ n", T1); If (BFS () printf ("the best score is % d. \ n ", ANS); else printf (" impossible \ n ");} return 0 ;}
Idea (2): BFS + DFS makes BFS for the whole castle, and BFS obtains a certain point (in fact, it contains three types, namely entrance, exit, and treasure) to another point (also the three types). Create an Implicit Graph during the BFS process, and then start the DFS search at the entrance to update ans.
Feeling: the pruning you want (it feels pretty strong) cannot pass, sweat! After reading the code on discuss, I added a pruning and optimized it to 31 Ms. DFS pruning: 1. Step> time direct return. 2. When ans = sum, you do not need to search again because it is already the largest. 3. If you find a point, it has already been searched before, and the jewelry value is less than before, and the number of steps is more than before, you don't need to search for it. (Let's take a look at the int VV [15], vs [15] In my code below. It is actually used to cut this branch. In fact, this can be done very quickly without any need)
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <queue> # define maxn 55 using namespace STD; int N, M, L, CNT, ANS, SUM; int Sx, Sy, ex, ey; int A [15]; int DX [] = {-,}; int dy [] =, -1, 1}; bool vis [maxn] [maxn]; char s [maxn]; char MP [maxn] [maxn]; int Dist [15] [15]; // construct an implicit graph for DFS 0 ~ 9 represents ~ J 10 ~ @ 11 ~ <Bool dvis [15]; int VV [15], vs [15]; struct node {int X, Y, step;} cur, now; queue <node> q; void BFS (int stx, int sty, int K) // find the shortest path in BFS {int I, j, NX, NY, TX, Ty, nstep, TMP; while (! Q. empty () Q. pop (); memset (VIS, 0, sizeof (VIS); cur. X = STX; cur. y = sty; cur. step = 0; q. push (cur); vis [STX] [sty] = 1; while (! Q. empty () {now = Q. front (); q. pop (); Nx = now. x; ny = now. y; nstep = now. step; If (nstep> L) break; If (nstep) {If (MP [NX] [NY]> = 'A' & MP [NX] [NY] <= 'J ') {TMP = MP [NX] [NY]-'A'; Dist [k] [TMP] = DIST [TMP] [k] = nstep ;} else if (MP [NX] [NY] = '@') {TMP = 10; Dist [k] [TMP] = DIST [TMP] [k] = nstep ;} else if (MP [NX] [NY] = '<') {TMP = 11; dist [k] [TMP] = DIST [TMP] [k] = nstep ;}}for (I = 0; I <4; I ++) {Tx = NX + dx [I]; ty = ny + dy [I]; If (MP [TX] [ty]! = '*'&&! Vis [TX] [ty]) {vis [TX] [ty] = 1; cur. X = TX; cur. y = ty; cur. step = nstep + 1; q. push (cur) ;}}} bool DFS (int K, int Val, int step) // DFS search answer {int I, j, flag = 0; if (Step> L | vv [k]> = Val & VS [k] <= Step | ans = sum) return false; If (k = 11) {If (ANS <Val) ans = val; return true ;}for (I = 0; I <= 11; I ++) {If (Dist [k] [I] &! Dvis [I]) {dvis [I] = 1; if (DFS (I, Val + A [I], step + dist [k] [I]) {flag = 1; if (VV [k] <Val) {VV [k] = val; vs [k] = Step ;}} dvis [I] = 0 ;}} if (FLAG) return true; return false;} int main () {int I, j, T, T1 = 0; scanf ("% d", & T ); while (t --) {T1 ++; sum = 0; scanf ("% d", & M, & N, & L, & CNT ); for (I = 0; I <CNT; I ++) {scanf ("% d", & A [I]); sum + = A [I];} A [10] = A [11] = 0; memset (MP, '*', sizeof (MP); memset (Dist, 0, sizeof (DIST )); for (I = 1; I <= N; I ++) {scanf ("% s", S); For (j = 1; j <= m; j ++) {MP [I] [J] = s [J-1]; If (s [J-1] = '@') SX = I, Sy = J; else if (s [J-1] = '<') EX = I, ey = J ;}} for (I = 1; I <= N; I ++) // find the shortest path of the vertex from each vertex {for (j = 1; j <= m; j ++) {If (MP [I] [J] = '@') BFS (I, j, 10 ); else if (MP [I] [J] = '<') BFS (I, j, 11 ); else if (MP [I] [J]> = 'A' & MP [I] [J] <= 'J') BFS (I, j, MP [I] [J]-'A') ;}} if (T1> 1) printf ("\ n"); printf ("case % d: \ n ", t1); ans =-1; memset (dvis, 0, siz EOF (dvis); memset (VV,-1, sizeof (vv); memset (Vs,-1, sizeof (VS); dvis [10] = 1; DFS (10, 0, 0); If (ANS! =-1) printf ("the best score is % d. \ n", ANS); else printf ("impossible \ n");} return 0 ;}