BFS+DFS(hdu 1044)

来源:互联网 发布:淘宝卖家怎么弄花呗 编辑:程序博客网 时间:2024/05/16 17:42


 

Collect More Jewels

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4709    Accepted Submission(s): 991

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 integerswhich 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 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:Impossible Case 3:The best score is 300.

 

 

题目大意:

在一个迷宫中,从起点走到终点,还有几个宝物,问在给定的时间内,到达终点后所能获取的最大价值。

思路:

先用bfs求出入口,宝物,出口,两两之间的最短距离。

在用dfs搜索所有情况,求出从入口走到出口能获得的最大价值。

熟悉两种搜索的优缺点:

BFS: 对于解决最短或最少问题特别有效,而且寻找深度小,但缺点是内存耗费量大(需要开大量的数组单元用来存储状态)。

DFS:对于解决遍历和求所有问题有效,对于问题搜索深度小的时候处理速度迅速,然而在深度很大的情况下效率不高

 

 

代码:

 

#include<stdio.h>#include<string.h>int t,w,h,l,m; char maze[55][55];                               //搜索地图 int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};int max,sum;                                       //max为最后可以获得的最多珠宝,sum为总的珠宝数 int jew[15];                                       //保存所有的珠宝,jew[0]为起始点,jew[m+1] int dis[60][60];                                   //起点、终点以及所有的珠宝所在的点到其他的点之间的距离 int que[1000000];                                 //广搜保存的队列 int front,rear;                                     //队列的头尾指针 int used[60][60];                                    //广搜时候保存当前节点是否走过 int step[60][60];                                   //从某一节点开始到其他点的路径 int vis[15];                                       //深搜的时候标记当前节点是否走过 void bfs(int x,int y,int s){front = rear = 0;memset(used,0,sizeof(used));memset(step,0,sizeof(step));int u = x*w+y;que[rear++] = u;used[x][y] = 1;step[x][y] = 0;while(front!=rear){int tmp = que[front];front++;for(int i=0;i<4;i++){int xx = tmp/w+dir[i][0];int yy = tmp%w+dir[i][1];if(xx<0||xx>=h||yy<0||yy>=w||maze[xx][yy]=='*'||used[xx][yy]==1)continue;used[xx][yy] = 1;step[xx][yy] = step[tmp/w][tmp%w]+1;if(maze[xx][yy]=='@'){dis[s][0] = step[xx][yy];} if(maze[xx][yy]=='<'){dis[s][m+1] = step[xx][yy];}if(maze[xx][yy]>='A'&&maze[xx][yy]<='J'){dis[s][maze[xx][yy]-'A'+1] = step[xx][yy];}que[rear++] = xx*w+yy;}}}void dfs(int s,int value,int time){if(time>l){return;}if(max==sum){return;}if(s>m){if(value>max){max = value;}return;}for(int i=0;i<=m+1;i++){if(dis[s][i]==0||vis[i]==1)continue;vis[i] = 1;dfs(i,value+jew[i],time+dis[s][i]);vis[i] = 0;}}int main(){scanf("%d",&t);int k = 1;while(t--){scanf("%d %d %d %d", &w,&h,&l,&m);memset(vis,0,sizeof(vis));memset(dis,0,sizeof(dis));sum = 0;for(int i=1;i<=m;i++)               //总的珠宝数 {scanf("%d",&jew[i]);sum += jew[i];}jew[0] = jew[m+1] = 0;           //起点和终点的珠宝为0 for(int i=0;i<h;i++){scanf("%s",maze[i]);}for(int i=0;i<h;i++){for(int j=0;j<w;j++){if(maze[i][j]=='@')          //从起点开始到其他各个点的距离 {bfs(i,j,0);}if(maze[i][j]=='<'){bfs(i,j,m+1);               //从终点到其他各个点的距离 }if(maze[i][j]>='A'&&maze[i][j]<='J'){bfs(i,j,maze[i][j]-'A'+1);          //从珠宝点到其他各个点的距离 }}}max = -1;vis[0] = 1;dfs(0,0,0);                         //从起点开始根据广搜保存的点进行深搜 printf("Case %d:\n",k);if(max>=0){printf("The best score is %d.\n",max);}else{printf("Impossible\n");}if(t!=0){printf("\n");}k++;}}


0 0