当前位置:   article > 正文

HDU 1044 Collect More Jewels【BFS+DFS+建立距离图】_collect more jewels java

collect more jewels java


Collect More Jewels

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


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.


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


思路:
先用bfs求出入口,宝物,出口,两两之间的最短距离。
在用dfs搜索所有情况,求出从入口走到出口能获得的最大价值。


熟悉两种搜索的优缺点:
BFS: 对于解决最短或最少问题特别有效,而且寻找深度小,但缺点是内存耗费量大(需要开大量的数组单元用来存储状态)。
DFS:对于解决遍历和求所有问题有效,对于问题搜索深度小的时候处理速度迅速,然而在深度很大的情况下效率不高

  1. #include<stdio.h>
  2. #include<queue>
  3. #include<iostream>
  4. #include<string.h>
  5. using namespace std;
  6. struct node
  7. {
  8. int x,y,step;
  9. };
  10. int maxx,n,m,many,limit_time,value_sum,val[15];
  11. bool vis2[15],vis[55][55];
  12. int step[4][2]= {1,0,-1,0,0,1,0,-1};
  13. char map[55][55];//原始地图
  14. int newmap[15][15];//新的最短距离标记 但是不是按照这个地图行走
  15. int check(int x,int y)
  16. {
  17. if(x>=0&&y>=0&&x<n&&y<m)
  18. return 1;
  19. return 0;
  20. }
  21. void BFS(int x,int y,int num)//num指的是: 0=@出发点 | many+1=< 出口点 | (0,many) 是标记‘A’到‘J’
  22. {
  23. queue<node>q;
  24. while(!q.empty())
  25. q.pop();
  26. node a,n;
  27. memset(vis,false,sizeof(vis));
  28. a.x=x;
  29. a.y=y;
  30. a.step=0;
  31. vis[x][y]=true;不加的话 newmap[][]是错误的 newmap中的0会变成2
  32. q.push(a);
  33. int nx,ny;
  34. while(!q.empty())
  35. {
  36. a=q.front();
  37. q.pop();
  38. for(int i=0; i<4; i++)
  39. {
  40. nx=a.x+step[i][0];
  41. ny=a.y+step[i][1];
  42. // if(nx>=0&&ny>=0&&nx<=n&&ny<=m)//不能白为什么 code block 不能这样编译 会出错,必须用check才行
  43. if(check(nx,ny))
  44. {
  45. if(map[nx][ny]!='*'&&vis[nx][ny]==false)
  46. {
  47. vis[nx][ny]=true;
  48. n.x=nx;
  49. n.y=ny;
  50. n.step=a.step+1;
  51. q.push(n);
  52. //找出num点到各个点的最短距离 标记给newmap
  53. if(map[nx][ny]=='@') newmap[num][0]=n.step;
  54. else if(map[nx][ny]=='<') newmap[num][many+1]=n.step;
  55. else if(isalpha(map[nx][ny])) newmap[num][map[nx][ny]-64]=n.step;
  56. }
  57. }
  58. }
  59. }
  60. }
  61. void DFS(int hang,int value,int step)//hang是指hang这个点 | value 总价值
  62. {
  63. if(step>limit_time||maxx==value_sum) return ;一个是> 不是>= && 另一个是maxx== 不是value==
  64. if(hang>many&&value>maxx)//已经找完最后一个宝珠 并且最后一个宝珠也被加到总价值里面了
  65. maxx=value; //选出每个深搜后的最大值
  66. for(int i=0; i<=many+1; i++)是<=不是<
  67. {
  68. if(vis2[i]==true||newmap[hang][i]==0) continue;
  69. vis2[i]=true;
  70. DFS(i,value+val[i],newmap[hang][i]+step);
  71. vis2[i]=false;
  72. }
  73. }
  74. int main(void)
  75. {
  76. int c,xxx=1;
  77. scanf("%d",&c);
  78. while(c--)
  79. {
  80. scanf("%d%d%d%d",&m,&n,&limit_time,&many);
  81. memset(vis2,false,sizeof(vis2));
  82. memset(newmap,0,sizeof(newmap));
  83. maxx=-1;
  84. value_sum=0;之前少输入了这个 一直过不了
  85. for(int i=1; i<=many; i++) //宝珠的数量
  86. {
  87. scanf("%d",&val[i]);
  88. value_sum+=val[i];
  89. }
  90. val[0]=val[many+1]=0;
  91. for(int i=0; i<n; i++)
  92. {
  93. scanf("%s",map[i]);不能吧下一个for放到这里面,因为下一个for里面进行了 搜索
  94. }
  95. //广搜BFS找出各个点之间的最短距离
  96. for(int i=0; i<n; i++)
  97. {
  98. for(int ii=0; ii<m; ii++)
  99. {
  100. if(map[i][ii]=='@') BFS(i,ii,0);
  101. else if(map[i][ii]=='<') BFS(i,ii,many+1);
  102. else if(isalpha(map[i][ii])) BFS(i,ii,map[i][ii]-64);//@在ascll码里刚好是A前面一个编号64 这里也可以 减-64
  103. }
  104. }
  105. /*
  106. for(int i=0; i<=many+3; i++)
  107. {
  108. for(int ii=0; ii<=many+3; ii++)
  109. {
  110. printf("%3d",newmap[i][ii]);
  111. }
  112. printf("\n");
  113. }
  114. */
  115. //深搜 找出符合条件最大价值的路径
  116. vis2[0]=true;
  117. DFS(0,0,0);
  118. //输出部分
  119. printf("Case %d:\n",xxx++);
  120. if(maxx==-1)
  121. printf("Impossible\n");
  122. else
  123. printf("The best score is %d.\n",maxx);
  124. if(c) printf("\n");
  125. }
  126. return 0;
  127. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/645506
推荐阅读
相关标签
  

闽ICP备14008679号