Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- /*
- Not fast enough.
- */
- public class MinTrianglePath4 {
- public static void main(String[] args) throws IOException {
- List<List<Node>> rows = null;
- try {
- rows = go();
- } catch (TriangleInputException e) {
- System.err.println(e.getMessage());
- System.exit(1);
- }
- PathResult minimumPath = evaluate(rows);
- if (minimumPath != null) {
- System.out.println("Minimum path is: " + minimumPath);
- } else {
- System.out.println("Failed to find the minimum path.");
- }
- }
- public static List<List<Node>> go() throws TriangleInputException {
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- String s;
- // Build binary tree from STDIN
- int rowIndex = 0;
- List<List<Node>> rows = new ArrayList<List<Node>>();
- try {
- while ((s = in.readLine()) != null) {
- s = s.trim();
- if ("".equals(s)) {
- continue;
- }
- // check right number of fields
- String[] values = s.split(" ");
- if (rows.size() > 0) {
- if (values.length != (rowIndex + 1)) {
- throw new TriangleInputException("Error on line " + (rowIndex + 1) + ". Expected " + (rowIndex + 1) + " values, got " + values.length + ".");
- }
- }
- List<Node> row = new ArrayList<Node>();
- for (int i = 0; i < values.length; i++) {
- Node n;
- try {
- n = new Node(new Integer(values[i]), rowIndex);
- } catch (NumberFormatException e) {
- throw new TriangleInputException("Error on line " + (rowIndex + 1) + ". Couldn't parse '" + values[i] + "' as an Integer.");
- }
- row.add(n);
- // work out parents for everything after the first row
- if (rows.size() > 0) {
- List<Node> rowAbove = rows.get(rows.size() - 1);
- Node parentLeft = null;
- Node parentRight = null;
- if (rowIndex > 0) {
- if (i == 0) {
- // right parent only
- parentRight = rowAbove.get(i);
- }
- else if (i == (values.length - 1)) {
- // left parent only
- parentLeft = rowAbove.get(i - 1);
- }
- else {
- parentLeft = rowAbove.get(i - 1);
- parentRight = rowAbove.get(i);
- }
- if (parentLeft != null) {
- parentLeft.setRightChild(n);
- }
- if (parentRight != null) {
- parentRight.setLeftChild(n);
- }
- }
- }
- }
- rows.add(row);
- rowIndex++;
- }
- } catch (IOException e) {
- throw new TriangleInputException("Error loading triangle input data.", e);
- }
- if (rows.size() == 0) {
- throw new TriangleInputException("No paths to evaluate.");
- }
- return rows;
- }
- public static PathResult evaluate(List<List<Node>> rows) {
- return traverse(rows.get(0).get(0), new ArrayList<Node>(), null);
- }
- public static PathResult traverse(Node n, List<Node> path, PathResult currentLowest) {
- // evaluate path. If it's already higher than currentLowest
- // then we can bail out of this path
- long result = 0;
- for (Node node : path) {
- result += node.getValue();
- }
- if (currentLowest != null && result > currentLowest.getResult()) {
- return currentLowest;
- }
- List<Node> deeper = new ArrayList<Node>(path.size() + 1);
- deeper.addAll(path);
- deeper.add(n);
- if (n.getLeftChild() != null) {
- currentLowest = traverse(n.getLeftChild(), deeper, currentLowest);
- }
- if (n.getRightChild() != null) {
- currentLowest = traverse(n.getRightChild(), deeper, currentLowest);
- }
- if (n.getLeftChild() == null && n.getRightChild() == null) {
- int r = 0;
- for (Node n2 : deeper) {
- r+= n2.getValue();
- }
- if (currentLowest == null) {
- return new PathResult(r, deeper);
- }
- if (r < currentLowest.getResult()) {
- return new PathResult(r, deeper);
- } else {
- return currentLowest;
- }
- }
- return currentLowest;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement