3 changed files with 147 additions and 0 deletions
@ -0,0 +1,128 @@
@@ -0,0 +1,128 @@
|
||||
import java.util.Arrays; |
||||
import java.util.Scanner; |
||||
import java.io.FileReader; |
||||
import java.io.BufferedReader; |
||||
import java.io.FileNotFoundException; |
||||
|
||||
/** |
||||
* Solve the maximum sum path problem |
||||
* using brute force. |
||||
* |
||||
* This was useful for discovering that |
||||
* every other technique I tried, using |
||||
* sums of subtriangles, etc., was flawed. |
||||
*/ |
||||
public class Problem018 { |
||||
|
||||
public static final String FILE = "p018_input.txt"; |
||||
|
||||
public static int max = 0; |
||||
public static String maxPath = ""; |
||||
|
||||
public String run() { |
||||
return new Integer(bruteForcePath()).toString(); |
||||
} |
||||
|
||||
|
||||
public static int bruteForcePath() { |
||||
try { |
||||
int[][] triangle = loadTriangle(); |
||||
int dim = triangle.length; |
||||
|
||||
boolean[] goright = new boolean[dim]; |
||||
bruteForce(0, goright, triangle); |
||||
|
||||
return max; |
||||
} catch(FileNotFoundException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
public static void bruteForce(int depth, boolean[] goright, int[][] triangle) { |
||||
int dim = triangle.length; |
||||
if(depth==dim) { |
||||
// Base case:
|
||||
// take the path dictated by z
|
||||
StringBuilder sb = new StringBuilder(); |
||||
int sum = 0; |
||||
int c = 0; |
||||
int r = 0; |
||||
sum += triangle[0][0]; |
||||
sb.append(triangle[0][0]+" "); |
||||
for(r = 1; r<goright.length; r++){ |
||||
if(goright[r-1]) { |
||||
c++; |
||||
} |
||||
sum += triangle[r][c]; |
||||
sb.append(triangle[r][c]+" "); |
||||
} |
||||
sb.append("= "+sum); |
||||
if(sum>max) { |
||||
max = sum; |
||||
maxPath = sb.toString(); |
||||
} |
||||
} else { |
||||
// Recursive case:
|
||||
// Call brute force method for both choices
|
||||
goright[depth] = true; |
||||
bruteForce(depth+1, goright, triangle); |
||||
goright[depth] = false; |
||||
bruteForce(depth+1, goright, triangle); |
||||
} |
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
public static boolean goLeft(int i, int j, int[][] triangle) { |
||||
int leftSubtreeSum = subtreeSum( i+1, j, triangle); |
||||
int rightSubtreeSum = subtreeSum( i+1, j+1, triangle); |
||||
return (leftSubtreeSum >= rightSubtreeSum); |
||||
} |
||||
|
||||
|
||||
public static int subtreeSum(int r, int c, int[][] triangle) { |
||||
int sum = 0; |
||||
int dim = triangle.length; |
||||
for(int i=0; r+i<dim; i++){ |
||||
for(int j=0; j<=i; j++) { |
||||
sum += triangle[r+i][c+j]; |
||||
} |
||||
} |
||||
return sum; |
||||
} |
||||
|
||||
|
||||
/** 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; |
||||
} |
||||
} |
||||
|
@ -0,0 +1,15 @@
@@ -0,0 +1,15 @@
|
||||
75 |
||||
95 64 |
||||
17 47 82 |
||||
18 35 87 10 |
||||
20 04 82 47 65 |
||||
19 01 23 75 03 34 |
||||
88 02 77 73 07 63 67 |
||||
99 65 04 28 06 16 70 92 |
||||
41 41 26 56 83 40 80 70 33 |
||||
41 48 72 33 47 32 37 16 94 29 |
||||
53 71 44 65 25 43 91 52 97 51 14 |
||||
70 11 33 28 77 73 17 78 39 68 17 57 |
||||
91 71 52 38 17 14 91 43 58 50 27 29 48 |
||||
63 66 04 68 89 53 67 30 73 16 69 87 40 31 |
||||
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 |
Loading…
Reference in new issue