Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. /*
  2. * Treasure Cave
  3. =============
  4.  
  5. Bessie's grandfather was a pirate who accumulated a great treasure
  6. chest of golden plunder. He hid the treasure chest in a cave that
  7. Bessie has recently discovered right on Farmer John's land! Just
  8. inside the cave's entrance she found a map that told her how to get
  9. the treasure.
  10.  
  11. The cave has P passages (3 <= P <= 5,000) conveniently numbered
  12. 1..P. The entrance is passage 1; the treasure is located in some
  13. reachable passage T (2 <= T <= P), whose value is supplied. Passages
  14. are all approximately the same length; each one leads to a split
  15. where hitherto unexplored numbered passages take the inquisitive
  16. cow deeper underground. No passage appears as the split from more
  17. than one passage, and the map contains a total of NS splits (1 <=
  18. NS <= 5,000).
  19.  
  20. Bessie wants to know both how far away from the entrance the treasure
  21. lies and also which passage numbers to take to get to the treasure.
  22.  
  23. Consider the schematic representation of a cave shown below. Passage
  24. numbers are shown close to the passage they name. For this example,
  25. the treasure is at the end of passage number 7:
  26.  
  27. 3/
  28. /
  29. +
  30. / \ /5
  31. 2/ 4\ /
  32. 1 / +
  33. ----+ 6\ #7 /11
  34. \ \ / /
  35. 13\ + +
  36. 8\ 10/ \
  37. \ / \12
  38. +
  39. 9\
  40. \
  41.  
  42. Bessie would have to traverse passages 1, 2, 4, 6, and 7 to get to
  43. the treasure, a total distance of 5 (which is simply the passage
  44. count).
  45.  
  46. The input file includes a set of lines, each with a passage number
  47. N (1 <= N <= P) and the two passages (B1 and B2; 1 <= B1 <= P; 1
  48. <= B2 <= P) that branch off from it. Some line in the input file
  49. will include passage number 1 and its two branches (for our example,
  50. passages 2 and 13; likewise, passage number 8 has two branches: 9
  51. and 10).
  52.  
  53. Tell Bessie how to get to the treasure.
  54.  
  55. PROBLEM NAME: tcave
  56.  
  57. INPUT FORMAT:
  58.  
  59. * Line 1: Line 1 contains three space-separated integers: P, NS, and T
  60.  
  61. * Lines 2..NS+1: Each line contains three space-separated integers: N,
  62. B1, and B2
  63.  
  64. SAMPLE INPUT:
  65.  
  66. 13 6 7
  67. 6 7 8
  68. 2 3 4
  69. 10 11 12
  70. 8 9 10
  71. 1 2 13
  72. 4 5 6
  73.  
  74. INPUT DETAILS:
  75.  
  76. This input describes the sample cave in text.
  77.  
  78. OUTPUT FORMAT:
  79.  
  80. * Line 1: The distance D from the entrance to the treasure
  81.  
  82. * Lines 2..D+1: Line i+1 contains a single integer that is next
  83. passage Bessie takes to get to the treasure.
  84.  
  85. SAMPLE OUTPUT:
  86.  
  87. 5
  88. 1
  89. 2
  90. 4
  91. 6
  92. 7
  93.  
  94. OUTPUT DETAILS:
  95.  
  96. As in the text.
  97. */
  98. package aStar.Week3.Day3;
  99.  
  100. import java.io.*;
  101. import java.util.*;
  102.  
  103. public class Tcave {
  104.  
  105. static int[][] adjL;
  106. static int P;
  107. static int NS;
  108. static int T;
  109. static boolean found = false;
  110. static ArrayList<Integer> path;
  111.  
  112. public static void main(String[] args) throws IOException {
  113. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  114. StringTokenizer st = new StringTokenizer(br.readLine());
  115.  
  116. P = Integer.parseInt(st.nextToken());
  117. NS = Integer.parseInt(st.nextToken());
  118. T = Integer.parseInt(st.nextToken());
  119.  
  120. //1 idx
  121. adjL = new int[P+1][2];
  122. for (int i=0; i<NS; i++){
  123. st = new StringTokenizer(br.readLine());
  124. int idx = Integer.parseInt(st.nextToken());
  125. adjL[idx][0] = Integer.parseInt(st.nextToken());
  126. adjL[idx][1] = Integer.parseInt(st.nextToken());
  127. }
  128.  
  129. path = new ArrayList<Integer>();
  130.  
  131. ArrayList<Integer> path = new ArrayList<Integer>();
  132. dfs(1,path);
  133. System.out.println(path.size());
  134. for (int i:path){
  135. System.out.println(i);
  136. }
  137.  
  138. // System.out.println("Took "+(double)(System.nanoTime() - startTime)/1000000000 + " s");
  139. br.close();
  140. }
  141. @SuppressWarnings("unchecked")
  142. public static void dfs(int cidx, ArrayList<Integer> path){
  143. if (cidx==T){
  144. path.add(T);
  145. path = (ArrayList<Integer>) path.clone();
  146. return;
  147. }
  148. //dead end
  149. if (adjL[cidx][0]==0 && adjL[cidx][1]==0){
  150. return;
  151. }
  152. path.add(cidx);
  153.  
  154. for (int i=0; i<2; i++){
  155. int next = adjL[cidx][i];
  156. dfs(next,path);
  157. }
  158. path.remove(path.size()-1);
  159. return;
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement