Collect More Jewels
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2932 Accepted Submission(s): 547
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.
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.
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.
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.
Source
Asia 2005, Hangzhou (Mainland China), Preliminary
Recommend JGShining
题目大意:
在一个迷宫中,从起点走到终点,还有几个宝物,问在给定的时间内,到达终点后所能获取的最大价值。
思路:
先用bfs求出入口,宝物,出口,两两之间的最短距离。
在用dfs搜索所有情况,求出从入口走到出口能获得的最大价值。
熟悉两种搜索的优缺点:
BFS: 对于解决最短或最少问题特别有效,而且寻找深度小,但缺点是内存耗费量大(需要开大量的数组单元用来存储状态)。
DFS:对于解决遍历和求所有问题有效,对于问题搜索深度小的时候处理速度迅速,然而在深度很大的情况下效率不高
这个程序效率还没有很高,好像有的可以1次bfs就把入口、宝物、出口两两的距离找出来的。继续改进吧。
少加了个val[0]=val[M+1]=0,导致纠结了一天。
做搜索题一定要细心,写错一个就很难出来了,尤其是正式比赛时,教训啊!
/* HDU 1044 Collect More Jewels 走迷宫,问在规定时间内从起点到终点最多可以收集到宝物的价值是多少。 先bfs找出所有宝物及起点和终点这些位置之间的最短距离,再起点dfs最大。 加个剪枝,如果已经拿到所有宝物了,就不再搜索。 AC G+ 31MS 560K */ #include<stdio.h> #include<iostream> #include<queue> #include<string.h> using namespace std; int W,H,L,M;//区域是H行W列的,L是时间限制,M是宝石的数量 int val[60];//宝石的价值,1-M char map[60][60]; bool used[60][60];//BFS时访问标记 bool vis[60];//dfs时访问标记 int step[60][60]; int ans;//结果 int sum;//所有的宝石的价值总和 int move[4][2]={{-1,0},{1,0},{0,1},{0,-1}}; int dis[60][60];//记录初始位置、各宝石和出口两两间的距离 queue<int>q; //从(x1,y1)点到其它点的距离,s(0-M+1)是该点编号,0表示初始位置 //M+1表示出口,1-M表示第i个宝物堆 void bfs(int x1,int y1,int s) { while(!q.empty())q.pop(); memset(used,false,sizeof(used)); memset(step,0,sizeof(step)); int u=x1*W+y1; q.push(u); used[x1][y1]=true; step[x1][y1]=0; while(!q.empty()) { u=q.front(); q.pop(); int x=u/W; int y=u%W; for(int i=0;i<4;i++) { int xx=x+move[i][0]; int yy=y+move[i][1]; if(xx<0||xx>=H||yy<0||yy>=W)continue; if(used[xx][yy]||map[xx][yy]=='*')continue; used[xx][yy]=true; step[xx][yy]=step[x][y]+1; if(map[xx][yy]=='@')dis[s][0]=step[xx][yy]; else if(map[xx][yy]=='<') dis[s][M+1]=step[xx][yy]; else if(map[xx][yy]>='A'&&map[xx][yy]<='J') dis[s][map[xx][yy]-'A'+1]=step[xx][yy]; q.push(xx*W+yy); } } } //dfs,s表示当前点,value表示获得的价值,time表示花费的时间 void dfs(int s,int value,int time) { if(time>L)return;//超出限制时间 if(ans==sum) return;//已经得到了最大的价值,剪枝(没有这个会超时) if(s>M) { if(value>ans)ans=value; return; } for(int i=0;i<=M+1;i++) { if(dis[s][i]==0||vis[i])continue; vis[i]=true; dfs(i,value+val[i],time+dis[s][i]); vis[i]=false; } } int main() { // freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T; scanf("%d",&T); int iCase=0; while(T--) { memset(dis,0,sizeof(dis)); iCase++; scanf("%d%d%d%d",&W,&H,&L,&M); sum=0; ans=-1; for(int i=1;i<=M;i++) { scanf("%d",&val[i]); sum+=val[i]; } val[0]=val[M+1]=0;//这个很重要啊,WR到死啊 for(int i=0;i<H;i++) scanf("%s",&map[i]); for(int i=0;i<H;i++) for(int j=0;j<W;j++) { if(map[i][j]=='@') bfs(i,j,0); else if(map[i][j]=='<') bfs(i,j,M+1); else if(map[i][j]>='A'&&map[i][j]<='J') bfs(i,j,map[i][j]-'A'+1); } memset(vis,false,sizeof(vis)); vis[0]=true; dfs(0,0,0); printf("Case %d:\n",iCase); if(ans>=0)printf("The best score is %d.\n",ans); else printf("Impossible\n"); if(T>0)printf("\n"); } return 0; }