Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6739 Accepted Submission(s): 1564
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.
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.
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.
#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include <queue> using namespace std; const int INF = 999999999; int n,m,limit,num; int dis[60][60]; ///记录两点之间的距离 int jw[60]; struct Node{ int x,y,step,geshu; }s; char graph[60][60]; bool vis[60][60]; int dir[][2]={{1,0},{-1,0},{0,1},{0,-1}}; bool check(int x,int y){ if(x<1||x>n||y<1||y>m||vis[x][y]||graph[x][y]=='*') return false; return true; } void bfs(Node s,int k){ queue<Node> q; vis[s.x][s.y] = true; s.geshu = 1; q.push(s); while(!q.empty()){ Node now = q.front(); q.pop(); if(now.geshu==num+2) return; for(int i=0;i<4;i++){ Node next; next.x = now.x+dir[i][0]; next.y = now.y+dir[i][1]; if(!check(next.x,next.y)) continue; char c = graph[next.x][next.y]; next.step = now.step+1; next.geshu = now.geshu+1; if(c=='.') next.geshu-=1; if(c=='@'){ dis[k][0] = next.step; } else if(c>='A'&&c<='J') { dis[k][c-'A'+1] = next.step; }else if(c=='<'){ dis[k][num+1] = next.step; } vis[next.x][next.y] = true; q.push(next); } } } bool vis1[60]; int MAX = 0,sum; void dfs(int u,int step,int ans){ if(step>limit || MAX == sum) return ; ///必须要加剪枝 if(u==num+1){ MAX = max(MAX,ans); return; } for(int i=0;i<=num+1;i++){ if(!vis1[i]){ vis1[i] = true; dfs(i,step+dis[u][i],ans+jw[i]); vis1[i] = false; } } } int main(){ int tcase; scanf("%d",&tcase); int kk = 1; while(tcase--){ for(int i=0;i<30;i++){ for(int j=0;j<30;j++){ dis[i][j] = (i==j)?0:INF; } } scanf("%d%d%d%d",&m,&n,&limit,&num); sum = 0; for(int i=1;i<=num;i++){ scanf("%d",&jw[i]); sum+=jw[i]; } jw[num+1] = jw[0] = 0; for(int i=1;i<=n;i++){ scanf("%s",graph[i]+1); } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(graph[i][j]=='@') { memset(vis,false,sizeof(vis)); s.x = i,s.y = j,s.step=0; bfs(s,0); } if(graph[i][j]=='<'){ memset(vis,false,sizeof(vis)); s.x = i,s.y = j,s.step=0; bfs(s,num+1); } if(graph[i][j]>='A'&&graph[i][j]<='J'){ memset(vis,false,sizeof(vis)); s.x = i,s.y = j,s.step=0; bfs(s,graph[i][j]-'A'+1); } } } printf("Case %d:\n",kk++); if(dis[0][num+1]>limit){ printf("Impossible\n"); if(tcase) printf("\n"); continue; } memset(vis1,false,sizeof(vis1)); MAX = 0; vis1[0] = true; dfs(0,0,0); printf("The best score is %d.\n",MAX); if(tcase) printf("\n"); } }