Collect More Jewels
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7881 Accepted Submission(s):
1886
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.
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.
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.
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.
、
bfs题目
1 #include<stdio.h> 2 #include<cstring> 3 #include<stdlib.h> 4 #include<map> 5 #include<cmath> 6 #include<queue> 7 #include<vector> 8 #include<iostream> 9 using namespace std; 10 bool vis[51][51][1<<10]; 11 char mp[52][52]; 12 int dis[4][2]= {{1,0},{-1,0},{0,1},{0,-1}}; 13 struct node 14 { 15 int x,y; 16 int step; 17 int val; 18 int state; 19 } cur,now; 20 int v[15],n,m,di,dj,ans,ti; 21 queue<node>q; 22 int bfs() 23 { 24 memset(vis,0,sizeof(vis)); 25 int nx,ny,nstep,nval,nst,tst,tmp; 26 ans=-1; 27 while(!q.empty())q.pop(); 28 cur.x=di,cur.y=dj,cur.step=0,cur.val=0,cur.state=0; 29 q.push(cur); 30 while(!q.empty()) 31 { 32 now=q.front(); 33 q.pop(); 34 nx=now.x; 35 ny=now.y; 36 nstep=now.step; 37 nval=now.val; 38 nst=now.state; 39 if(nstep>ti) break ; 40 if(mp[nx][ny]=='<') 41 { 42 ans=max(ans,nval); 43 } 44 for(int i=0; i<4; i++) 45 { 46 int ni=nx+dis[i][0]; 47 int nj=ny+dis[i][1]; 48 tst=nst; 49 cur.val=nval; 50 if(mp[ni][nj]>='A'&&mp[ni][nj]<='J') 51 { 52 tmp=mp[ni][nj]-'A'; 53 if(!(tst&(1<<tmp))) cur.val+=v[tmp]; 54 tst=tst|(1<<tmp); 55 } 56 if(ni>=0&&ni<m&&nj>=0&&nj<n&&mp[ni][nj]!='*'&&vis[ni][nj][tst]==0) 57 { 58 vis[ni][nj][tst]=1; 59 cur.x=ni; 60 cur.y=nj; 61 cur.step=nstep+1; 62 cur.state=tst; 63 q.push(cur); 64 } 65 } 66 } 67 if(ans==-1)return true; 68 return false; 69 } 70 int main() 71 { 72 int sh,t,t1=0; 73 scanf("%d",&t); 74 while(t--) 75 { 76 t1++; 77 scanf("%d%d%d%d",&n,&m,&ti,&sh); 78 for(int i=0; i<sh; i++) 79 scanf("%d",&v[i]); 80 for(int i=0; i<m; i++) 81 { 82 getchar(); 83 scanf("%s",mp[i]); 84 for(int j=0; j<n; j++) 85 { 86 if(mp[i][j]=='@') 87 { 88 di=i; 89 dj=j; 90 } 91 } 92 } 93 /* for(int i=0;i<m;i++) 94 { 95 printf("%s ",mp[i]); 96 } 97 */ 98 if(t1>1) printf(" "); 99 printf("Case %d: ",t1); 100 if(!bfs()) printf("The best score is %d. ",ans); 101 else printf("Impossible "); 102 } 103 return 0; 104 }