当前位置:   article > 正文

HDU1044 Collect More Jewels_it is written in the book of the lady: after the c

it is written in the book of the lady: after the creation, the cruel god mol
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
  1. import java.util.*;
  2. public class HDU1044 {
  3. /**
  4. * @param args
  5. */
  6. static int n, m;
  7. static int max = 0, sum = 0;
  8. static int totalTime, ex, ey;
  9. static int dirArr[][] = new int[][] { { 1, 0 }, { 0, 1 }, { -1, 0 },
  10. { 0, -1 } };
  11. static char[][] map;
  12. static int[] value;
  13. static int[][] adj = new int[14][14];
  14. static boolean visit[];
  15. static boolean visadj[][];
  16. static Node[] nodes;
  17. static int num;
  18. public static void main(String[] args) {
  19. Scanner sc = new Scanner(System.in);
  20. int T = sc.nextInt();
  21. for (int i = 1; i <= T; i++) {
  22. max = 0;
  23. sum = 0;
  24. nodes = new Node[13];
  25. visit = new boolean[15];
  26. m = sc.nextInt();
  27. n = sc.nextInt();
  28. totalTime = sc.nextInt();
  29. num = sc.nextInt();
  30. value = new int[num];
  31. map = new char[n][m];
  32. int sx = 0, sy = 0;
  33. for (int k = 0; k < num; k++)
  34. value[k] = sc.nextInt();
  35. for (int k = 0; k < n; k++) {
  36. char[] arr = sc.next().toCharArray();
  37. for (int j = 0; j < m; j++) {
  38. char temp = arr[j];
  39. map[k][j] = temp;
  40. if (temp == '@') {
  41. sx = k;
  42. sy = j;
  43. } else if (temp == '<') {
  44. ex = k;
  45. ey = j;
  46. } else if (temp >= 'A' && temp <= 'J') {
  47. nodes[temp - 'A'] = new Node(k, j, 0);
  48. }
  49. }
  50. }
  51. InitArray();
  52. BFS(sx, sy, num);
  53. if (i > 1)
  54. System.out.println();
  55. System.out.println("Case " + i + ":");
  56. if (adj[num][num + 1] == -1 || adj[num][num + 1] > totalTime) {
  57. System.out.println("Impossible");
  58. continue;
  59. }
  60. for (int kk = 0; kk < num; kk++) {
  61. if (adj[kk][num] != -1)
  62. sum += value[kk];
  63. }
  64. BFS(ex, ey, num + 1);
  65. for (int k = 0; k < num; k++) {
  66. BFS(nodes[k].x, nodes[k].y, k);
  67. }
  68. visit[num] = true;
  69. DFS(num, 0, 0);
  70. System.out.println("The best score is " + max + ".");
  71. }
  72. }
  73. static void InitArray() {
  74. for (int i = 0; i < 14; i++)
  75. for (int j = 0; j < 14; j++)
  76. adj[i][j] = -1;
  77. }
  78. static void DFS(int i, int step, int val) {
  79. if (step > totalTime || max == sum)
  80. return;
  81. if (i == num + 1) {
  82. if (val > max)
  83. max = val;
  84. }
  85. if (i != num + 1 && step + adj[i][num + 1] > totalTime)
  86. return;
  87. for (int j = 0; j <= num + 1; j++) {
  88. if (adj[i][j] != -1) {
  89. if (visit[j])
  90. continue;
  91. visit[j] = true;
  92. if (j < num) {
  93. DFS(j, step + adj[i][j], val + value[j]);
  94. } else {
  95. DFS(j, step + adj[i][j], val);
  96. }
  97. visit[j] = false;
  98. }
  99. }
  100. }
  101. static void BFS(int x0, int y0, int k) {
  102. Queue<Node> queue = new ArrayDeque<Node>();
  103. queue.add(new Node(x0, y0, 0));
  104. visadj = new boolean[55][55];
  105. visadj[x0][y0] = true;
  106. while (!queue.isEmpty()) {
  107. Node pre = queue.poll();
  108. for (int i = 0; i < 4; i++) {
  109. Node curr = new Node(pre.x, pre.y, pre.step);
  110. curr.x += dirArr[i][0];
  111. curr.y += dirArr[i][1];
  112. int x = curr.x;
  113. int y = curr.y;
  114. curr.step++;
  115. if (x < 0 || x >= n || y < 0 || y >= m || map[x][y] == '*'
  116. || visadj[x][y])
  117. continue;
  118. visadj[x][y] = true;
  119. queue.add(curr);
  120. if (map[x][y] >= 'A' && map[x][y] <= 'J') {
  121. int index = map[x][y] - 'A';
  122. adj[index][k] = adj[k][index] = curr.step;
  123. } else if (map[x][y] == '@') {
  124. adj[k][num] = adj[num][k] = curr.step;
  125. } else if (map[x][y] == '<') {
  126. adj[k][num + 1] = adj[num + 1][k] = curr.step;
  127. }
  128. }
  129. }
  130. }
  131. static class Node {
  132. protected int x, y;
  133. protected int step;
  134. protected boolean isVisit;
  135. public Node(int x, int y, int step) {
  136. this.x = x;
  137. this.y = y;
  138. this.step = step;
  139. }
  140. }
  141. }

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

闽ICP备14008679号