-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask15.java
189 lines (148 loc) · 4.72 KB
/
Task15.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
public class Task15 {
public static void main(String[] args) {
// prepare to read the file
Scanner in = new Scanner(System.in);
String inLine;
// get the number of rows in this honeycomb
int N = Integer.parseInt(in.nextLine());
// loop through the rows and create the nodes
Node[][] nodes = new Node[N][N];
for (int i=0; i<N; i++) {
// read the line of text
inLine = in.nextLine();
// break it up
String[] tokens = inLine.split(",");
int numNodes = i+1;
// create nodes
for (int j=0; j<numNodes; j++) {
nodes[i][j] = new Node(tokens[j]);
}
}
// now nodes are created - link them together
for (int r=0; r<N; r++) {
for (int c=0; c<=r; c++) {
// get the current node
Node node = nodes[r][c];
if (c > 0) {
node.setL(nodes[r][c-1]);
node.setUL(nodes[r-1][c-1]);
}
if (c < r) {
node.setUR(nodes[r-1][c]);
node.setR(nodes[r][c+1]);
}
if (r < (N-1)) {
node.setDR(nodes[r+1][c+1]);
node.setDL(nodes[r+1][c]);
}
}
}
// start nodes
Node[] start = new Node[3];
start[0] = nodes[0][0];
start[1] = nodes[N-1][N-1];
start[2] = nodes[N-1][0];
// end nodes
Node[] end = new Node[3];
end[0] = nodes[N-1][N-1];
end[1] = nodes[N-1][0];
end[2] = nodes[0][0];
// best distances
int[] best = new int[3];
Arrays.fill(best, 0);
// take 3 trips
for (int trip=0; trip<3; trip++) {
// reset the nodes
for (int i=0; i<N; i++) {
for (int j=0; j<i+1; j++) {
nodes[i][j].setBest(Integer.MAX_VALUE);
}
}
// start at the end
end[trip].setBest(end[trip].getCost());
// queue it up
LinkedList<Node> queue = new LinkedList<Node>();
queue.add(end[trip]);
end[trip].setQueued(true);
// chew through the queue
while (!queue.isEmpty()) {
// get the next node
Node currentNode = queue.pop();
currentNode.setQueued(false);
// push the best
currentNode.publishBest(queue);
}
// remember the best
best[trip] = start[trip].getBest() - start[trip].getCost();
}
int grandTotal = best[0] + best[1] + best[2];
System.out.println(grandTotal);
}
}
class Node {
// cost to step in this node
private int cost;
// the best path distance
private int best;
// connections to other nodes: L, UL, UR, R, DR, DL
private Node[] connections;
private boolean queued;
public Node(String costStr) {
cost = Integer.parseInt(costStr);
connections = new Node[6];
Arrays.fill(connections, null);
best = Integer.MAX_VALUE;
queued = false;
}
public void setL(Node n) {
connections[0] = n;
}
public void setUL(Node n) {
connections[1] = n;
}
public void setUR(Node n) {
connections[2] = n;
}
public void setR(Node n) {
connections[3] = n;
}
public void setDR(Node n) {
connections[4] = n;
}
public void setDL(Node n) {
connections[5] = n;
}
public int getCost() {
return cost;
}
public int getBest() {
return best;
}
public void setBest(int newBest) {
best = newBest;
}
public void publishBest(LinkedList<Node> queue) {
for (Node node : connections) {
if (node != null) {
node.receiveBest(best, queue);
}
}
}
public void receiveBest(int newBest, LinkedList<Node> queue) {
int total = newBest + cost;
if (total <= best) {
// we have a new best
best = total;
if (!queued) queue.add(this);
}
}
public boolean isQueued() {
return queued;
}
public void setQueued(boolean queued) {
this.queued = queued;
}
}