|
|
|
@ -1,13 +1,7 @@
@@ -1,13 +1,7 @@
|
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.Scanner; |
|
|
|
|
import java.io.FileReader; |
|
|
|
|
import java.io.BufferedReader; |
|
|
|
|
import java.io.FileNotFoundException; |
|
|
|
|
|
|
|
|
|
public class Path { |
|
|
|
|
|
|
|
|
|
public static final String FILE = "triangle_counterexample1.txt"; |
|
|
|
|
|
|
|
|
|
/** Find the maximum path down a triangle. |
|
|
|
|
* |
|
|
|
|
* Strategy: add up the marginal sums of each diagonal, |
|
|
|
@ -15,14 +9,14 @@ public class Path {
@@ -15,14 +9,14 @@ public class Path {
|
|
|
|
|
* and use those measures to make decisions about which |
|
|
|
|
* directions to move in. |
|
|
|
|
*/ |
|
|
|
|
public static void main(String[] args) throws FileNotFoundException { |
|
|
|
|
public static void main(String[] args) { |
|
|
|
|
System.out.println(navigateMaxPath()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Navigate the triangle one step at a time, making decisions as we go about which direction to move. */ |
|
|
|
|
public static int navigateMaxPath() throws FileNotFoundException { |
|
|
|
|
int[][] triangle = loadTriangle(); |
|
|
|
|
public static int navigateMaxPath() { |
|
|
|
|
int[][] triangle = LoadTriangle.loadTriangle(); |
|
|
|
|
int dim = triangle.length; |
|
|
|
|
|
|
|
|
|
int crawl = 0; |
|
|
|
@ -33,8 +27,8 @@ public class Path {
@@ -33,8 +27,8 @@ public class Path {
|
|
|
|
|
System.out.print(triangle[rr][cc] + " "); |
|
|
|
|
if(rr<dim-1 && cc<dim-1) { |
|
|
|
|
System.out.println(); |
|
|
|
|
System.out.println(" left sum = "+subtreeSum(rr+1,cc,triangle) ); |
|
|
|
|
System.out.println(" right sum = "+subtreeSum(rr+1,cc+1,triangle) ); |
|
|
|
|
System.out.println(" left sum = " +subtriangleSum(rr+1,cc,triangle) ); |
|
|
|
|
System.out.println(" right sum = "+subtriangleSum(rr+1,cc+1,triangle) ); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(goLeft(rr,cc,triangle)) { |
|
|
|
@ -50,14 +44,14 @@ public class Path {
@@ -50,14 +44,14 @@ public class Path {
|
|
|
|
|
|
|
|
|
|
/** Determine whether we should go left at row i, column j of the triangle. */ |
|
|
|
|
public static boolean goLeft(int i, int j, int[][] triangle) { |
|
|
|
|
int leftSubtreeSum = subtriangleSum( i+1, j, triangle); |
|
|
|
|
int rightSubtreeSum = subtriangleSum( i+1, j+1, triangle); |
|
|
|
|
int leftSum = subtriangleSum( i+1, j, triangle); |
|
|
|
|
int rightSum = subtriangleSum( i+1, j+1, triangle); |
|
|
|
|
int dim = triangle.length; |
|
|
|
|
// break ties by value, but skip last row
|
|
|
|
|
if(leftSubtreeSum==rightSubtreeSum && i+1>dim) { |
|
|
|
|
if(leftSum==rightSum && i+1>dim) { |
|
|
|
|
return (triangle[i+1][j] > triangle[i+1][j+1]); |
|
|
|
|
} else { |
|
|
|
|
return leftSubtreeSum > rightSubtreeSum; |
|
|
|
|
return leftSum > rightSum; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -107,34 +101,4 @@ public class Path {
@@ -107,34 +101,4 @@ public class Path {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Load a triangle from a file. */ |
|
|
|
|
public static int[][] loadTriangle() throws FileNotFoundException { |
|
|
|
|
int nlines = countLines(); |
|
|
|
|
int[][] triangle = new int[nlines][nlines]; |
|
|
|
|
Scanner s = new Scanner(new BufferedReader(new FileReader(FILE))); |
|
|
|
|
int r = 0; |
|
|
|
|
while(s.hasNextLine()) { |
|
|
|
|
String line = s.nextLine(); |
|
|
|
|
Scanner ls = new Scanner(line); |
|
|
|
|
int c = 0; |
|
|
|
|
while(ls.hasNextInt()) { |
|
|
|
|
int num = ls.nextInt(); |
|
|
|
|
triangle[r][c] = num; |
|
|
|
|
c++; |
|
|
|
|
} |
|
|
|
|
r++; |
|
|
|
|
} |
|
|
|
|
return triangle; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** Count the number of lines in the triangle file. */ |
|
|
|
|
public static int countLines() throws FileNotFoundException { |
|
|
|
|
Scanner s = new Scanner(new BufferedReader(new FileReader(FILE))); |
|
|
|
|
int lines = 0; |
|
|
|
|
while(s.hasNextLine()) { |
|
|
|
|
s.nextLine(); |
|
|
|
|
lines++; |
|
|
|
|
} |
|
|
|
|
return lines; |
|
|
|
|
} |
|
|
|
|
} |