Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4.  
  5. /*
  6. Not fast enough.
  7. */
  8.  
  9. public class MinTrianglePath4 {
  10.  
  11. public static void main(String[] args) throws IOException {
  12.  
  13. List<List<Node>> rows = null;
  14. try {
  15. rows = go();
  16. } catch (TriangleInputException e) {
  17. System.err.println(e.getMessage());
  18. System.exit(1);
  19. }
  20.  
  21. PathResult minimumPath = evaluate(rows);
  22. if (minimumPath != null) {
  23. System.out.println("Minimum path is: " + minimumPath);
  24. } else {
  25. System.out.println("Failed to find the minimum path.");
  26. }
  27.  
  28. }
  29.  
  30.  
  31. public static List<List<Node>> go() throws TriangleInputException {
  32.  
  33. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  34. String s;
  35.  
  36. // Build binary tree from STDIN
  37. int rowIndex = 0;
  38. List<List<Node>> rows = new ArrayList<List<Node>>();
  39. try {
  40. while ((s = in.readLine()) != null) {
  41.  
  42. s = s.trim();
  43. if ("".equals(s)) {
  44. continue;
  45. }
  46.  
  47. // check right number of fields
  48. String[] values = s.split(" ");
  49. if (rows.size() > 0) {
  50. if (values.length != (rowIndex + 1)) {
  51. throw new TriangleInputException("Error on line " + (rowIndex + 1) + ". Expected " + (rowIndex + 1) + " values, got " + values.length + ".");
  52. }
  53. }
  54.  
  55.  
  56. List<Node> row = new ArrayList<Node>();
  57. for (int i = 0; i < values.length; i++) {
  58. Node n;
  59. try {
  60. n = new Node(new Integer(values[i]), rowIndex);
  61. } catch (NumberFormatException e) {
  62. throw new TriangleInputException("Error on line " + (rowIndex + 1) + ". Couldn't parse '" + values[i] + "' as an Integer.");
  63. }
  64. row.add(n);
  65.  
  66. // work out parents for everything after the first row
  67. if (rows.size() > 0) {
  68. List<Node> rowAbove = rows.get(rows.size() - 1);
  69. Node parentLeft = null;
  70. Node parentRight = null;
  71.  
  72. if (rowIndex > 0) {
  73.  
  74. if (i == 0) {
  75. // right parent only
  76. parentRight = rowAbove.get(i);
  77. }
  78. else if (i == (values.length - 1)) {
  79. // left parent only
  80. parentLeft = rowAbove.get(i - 1);
  81. }
  82. else {
  83. parentLeft = rowAbove.get(i - 1);
  84. parentRight = rowAbove.get(i);
  85. }
  86.  
  87. if (parentLeft != null) {
  88. parentLeft.setRightChild(n);
  89. }
  90. if (parentRight != null) {
  91. parentRight.setLeftChild(n);
  92. }
  93. }
  94.  
  95. }
  96.  
  97. }
  98. rows.add(row);
  99. rowIndex++;
  100. }
  101. } catch (IOException e) {
  102. throw new TriangleInputException("Error loading triangle input data.", e);
  103. }
  104.  
  105. if (rows.size() == 0) {
  106. throw new TriangleInputException("No paths to evaluate.");
  107. }
  108.  
  109. return rows;
  110. }
  111.  
  112.  
  113. public static PathResult evaluate(List<List<Node>> rows) {
  114. return traverse(rows.get(0).get(0), new ArrayList<Node>(), null);
  115. }
  116.  
  117. public static PathResult traverse(Node n, List<Node> path, PathResult currentLowest) {
  118.  
  119. // evaluate path. If it's already higher than currentLowest
  120. // then we can bail out of this path
  121. long result = 0;
  122. for (Node node : path) {
  123. result += node.getValue();
  124. }
  125.  
  126. if (currentLowest != null && result > currentLowest.getResult()) {
  127. return currentLowest;
  128. }
  129.  
  130.  
  131. List<Node> deeper = new ArrayList<Node>(path.size() + 1);
  132. deeper.addAll(path);
  133. deeper.add(n);
  134.  
  135. if (n.getLeftChild() != null) {
  136. currentLowest = traverse(n.getLeftChild(), deeper, currentLowest);
  137. }
  138.  
  139. if (n.getRightChild() != null) {
  140. currentLowest = traverse(n.getRightChild(), deeper, currentLowest);
  141. }
  142.  
  143. if (n.getLeftChild() == null && n.getRightChild() == null) {
  144.  
  145. int r = 0;
  146. for (Node n2 : deeper) {
  147. r+= n2.getValue();
  148. }
  149.  
  150. if (currentLowest == null) {
  151. return new PathResult(r, deeper);
  152. }
  153.  
  154. if (r < currentLowest.getResult()) {
  155. return new PathResult(r, deeper);
  156. } else {
  157. return currentLowest;
  158. }
  159. }
  160.  
  161. return currentLowest;
  162. }
  163.  
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement