Hdu.1044.collect More Jewels (BFS + state compression)

Source: Internet
Author: User

Collect More Jewels

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) total submission (s): 5345 Accepted Submission (s): 1189

Problem DescriptionIt is written in the book of the Lady:after the Creation, the cruel god Moloch rebelled against the AU Thority of Marduk the Creator.moloch stole from Marduk the very powerful of all the artifacts of the gods, the Amulet of Y Endor, and he hid it in the dark cavities of gehennom, the under World, where he is 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, has been heralded from birth as the instrument of the Lady. You is 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 had ever played the computer game NetHack, you must was familiar with the quotes above. If you have never heard of it, does not worry. You'll learn it (and love it) soon.
In this problem, you, the adventurer, is in a dangerous dungeon. You were informed that the dungeon was going to collapse. You must find the exit stairs within given time. However, you don't want to leave the dungeon empty handed. There is lots of rare jewels in the dungeon. Try collecting some of them before you leave. Some of the jewels are cheaper and Some is more expensive. So you'll try your best to maximize your collection, more importantly, leave the dungeon in time.

Inputstandard input would contain multiple test cases. The first line of the input was a single integer t (1 <= t <=) which is the number of the test cases. T test Cases Follow, each preceded to a single blank line.
The first line of all test case contains four integers W (1 <= w <=), H (1 <= H <=), 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 was 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 Insi De the dungeon and is a wall. Time starts at 1 when the game begins. M is the number of jewels in the dungeon. Jewels'll be collected once, the adventurer is, and that block. This does isn't cost extra time.
The next line contains M Integers,which is the values of the jewels.
The next H lines would contain W characters each. They represent the dungeon map in the following notation: > [*] marks a wall, into which your can not move; > [.] Marks an empty space, to which you can move; > [@] marks the initial position of the adventurer; > [<] marks the exit stairs; > [A]-[J] marks the jewels.

Outputresults should is directed to standard output. Start each case with a ' case #: ' On a ', where # is the case number starting from 1. The consecutive cases should is separated by a and blank line. No blank line should is produced after the last Test case.
If The adventurer can make it to the exit stairs with the time limit, print the sentence "The best score are S.", where S is The maximum value of the jewels he can collect along the; Otherwise print the word "impossible" on a.

Sample Input34 4 2 2100 200*****@a**b<*****4 4 1 2100 200*****@a**b<*****12 5 2100 200*************b.........**.* *******.**@... a....<*************

Sample outputcase 1:the Best score are 200.Case 2:impossiblecase 3:the best score is 300.
1#include <stdio.h>2#include <queue>3#include <string.h>4#include <ctype.h>5#include <algorithm>6#include <math.h>7 intT, MAXN;8 intm, N, L, p;9 intval[ the] ;Ten Charmap[ -][ -] ; One BOOLvis[ -][ -][4000] ; A intmove[][2] = {{1,0} ,{-1,0} , {0,1} , {0,-1}} ; - structnode - { the     intx, y, step; -     intJew, V; - }; -  + voidBFS (intSxintSy) - { +Std::queue <node>Q; A      while( !q.empty ()) Q.pop (); atQ.push (node) {SX, SY,0,0,0}) ; - node ans, tmp; -vis[sx][sy][0] =1 ; -      while(!Q.empty ()) { -Ans =Q.front (); Q.pop (); -       //printf ("S---(%d,%d)%d =%d\n", ans.x, Ans.y, Ans.step, ans.v); in         if(Map[ans.x][ans.y] = ='<') MAXN =Std::max (MAXN, ans.v); -          for(inti =0; I <4; i + +) { totmp.x = ans.x + move[i][0] ; TMP.Y = Ans.y + move[i][1] ; +             if(Tmp.x <0|| Tmp.y <0|| tmp.x >= N | | TMP.Y >= m)Continue ; -             if(MAP[TMP.X][TMP.Y] = ='*')Continue ; theTmp.jew =Ans.jew; *Tmp.step = Ans.step +1 ; $             if(Tmp.step > L)Continue ;Panax Notoginseng             intK =-1 ; -Tmp.jew = Ans.jew; TMP.V =ans.v; the             if(Isalpha (map[tmp.x][tmp.y])) { +K = Map[tmp.x][tmp.y]-'A' ; A                     if(! (Tmp.jew & (1<< k)) TMP.V + =Val[k]; theTmp.jew = Tmp.jew | (1<<k); +             } -             if(Vis[tmp.x][tmp.y][tmp.jew])Continue ; $           //printf ("(%d,%d)%d =%d\n", tmp.x, Tmp.y, Tmp.step, TMP.V); $Vis[tmp.x][tmp.y][tmp.jew] =1 ; - Q.push (TMP); -         } the     } - }Wuyi  the intMain () - { Wu   //freopen ("A.txt", "R", stdin); -     intT, CAS =1 ; Aboutscanf ("%d", &T); $      while(T--) { -printf ("Case %d:\n", CAS + +) ; -         intex, EY, SX, SY; -memset (Vis,0,sizeof(Vis)); Ascanf ("%d%d%d%d", &m, &n, &l, &p); +          for(inti =0; I < P; i + +) scanf ("%d", &val[i]); the          for(inti =0; I < n; i + +) scanf ("%s", Map[i]);//, puts (Map[i]); -MAXN =-1 ; $          for(inti =0; I < n; i + +) { the              for(intj =0; J < M; J + +) { the                 if(Map[i][j] = ='@') theSX = i, sy =J; the                 Else if(Map[i][j] = ='<') -ex = i, EY =J; in             } the         } the         if(Fabs (Sx-ex) + fabs (Sy-ey) >l) { Aboutprintf ("impossible\n") ; the             if(T! =0) puts ("") ; the             Continue ; the         } + BFS (SX, SY); -         if(MAXN! =-1) printf ("The best score is%d.\n", MAXN); the         ElsePuts ("Impossible") ;Bayi         if(T! =0) puts ("") ; the     } the     return 0 ; -}
View Code

Tease me the shape of the pressure state | (1 << k) has been written as State | K, and then ....
There is a way to tease out a + treasure, after deciding to determine whether it has been added, and then directly +. (Obviously feel very water problem Orz)

Hdu.1044.collect More Jewels (BFS + state compression)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.