-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuler67.java
75 lines (54 loc) · 1.61 KB
/
Euler67.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.io.*;
import java.util.*;
class Node {
int value;
Node touch1;
Node touch2;
long distance;
Node(int value) {
this.value = value;
}
}
class Euler67 {
public static void main(String arg[]) throws IOException {
File file = new File("Euler67.txt");
Scanner scan = new Scanner(file);
int length = 100;
Node[][] pattern = new Node[length][];
long max = 0;
for(int i = 0; i < length; i++) {
pattern[i] = new Node[i+1];
for(int j = 0; j <= i; j++)
pattern[i][j] = new Node(scan.nextInt());
}
for(int i = 0; i < length-1; i++)
for(int j = 0; j <= i; j++) {
pattern[i][j].touch1 = pattern[i+1][j];
pattern[i][j].touch2 = pattern[i+1][j+1];
}
pattern[0][0].distance = pattern[0][0].value;
setDistance(pattern[0][0]);
for(int j = 0; j < length; j++)
if(pattern[length-1][j].distance > max)
max = pattern[length-1][j].distance;
System.out.println(max);
}
public static void setDistance(Node n) {
Node travel;
Queue<Node> q = new LinkedList<Node>();
q.add(n);
while(q.peek() != null) {
travel = q.remove();
if(travel.touch1 == null)
continue;
if(travel.touch1.distance < (travel.distance + travel.touch1.value)) {
travel.touch1.distance = travel.distance + travel.touch1.value;
q.add(travel.touch1);
}
if(travel.touch2.distance < (travel.distance + travel.touch2.value)) {
travel.touch2.distance = travel.distance + travel.touch2.value;
q.add(travel.touch2);
}
}
}
}