-
Notifications
You must be signed in to change notification settings - Fork 0
/
Naloga10.java
226 lines (191 loc) · 6.48 KB
/
Naloga10.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Naloga10 {
private String inputFilename;
private String outputFilename;
private BufferedReader reader;
private PrintWriter writer;
private int numberOfGroups;
private List<Point> points;
private List<Distance> distances = new ArrayList<>();
private int numberOfPoints;
private ArrayList<Group> groups;
private int numberOfAvailableGroups;
public Naloga10(String inputFilename, String outputFilename) {
this.inputFilename = inputFilename;
this.outputFilename = outputFilename;
}
public static void main(String[] args) throws IOException {
Naloga10 program = new Naloga10(args[0], args[1]);
program.initialise();
program.run();
program.cleanUp();
}
private void run() throws IOException {
readInput();
calculateDistancesBetweenEachPoint();
sortDistances();
do {
calculateSmallestDistance();
} while (groups.size() > numberOfAvailableGroups);
for (Group group : groups) {
group.sort();
}
sortGroups();
print();
}
public void sortGroups() {
groups.sort(Comparator.comparingInt(o -> o.getPoints().get(0).getLabel()));
}
public void readInput() throws IOException {
numberOfPoints = Integer.parseInt(reader.readLine());
points = new ArrayList<>(numberOfPoints);
groups = new ArrayList<>(numberOfPoints);
for (int i = 0; i < numberOfPoints; i++) {
String coordinatesString = reader.readLine();
String[] coordinatesStrings = coordinatesString.split(",");
double x = Double.parseDouble(coordinatesStrings[0]);
double y = Double.parseDouble(coordinatesStrings[1]);
Point point = new Point(x, y, i + 1);
points.add(point);
Group group = new Group(point);
point.setGroup(group);
groups.add(group);
}
numberOfGroups = Integer.parseInt(reader.readLine());
numberOfAvailableGroups = numberOfGroups;
}
public void calculateSmallestDistance() {
Distance smallestDistance = distances.get(0);
groupPoints(smallestDistance);
distances.remove(0);
}
private void groupPoints(Distance smallestDistance) {
Point point1 = points.get(smallestDistance.getPoint1Index());
Group point1Group = point1.getGroup();
Point point2 = points.get(smallestDistance.getPoint2Index());
Group point2Group = point2.getGroup();
if (!point1Group.equals(point2Group)) {
point1Group.addAllPoints(point2Group);
for (Point point : point2Group.getPoints()) {
point.setGroup(point1Group);
}
groups.remove(point2Group);
}
}
public void calculateDistancesBetweenEachPoint() {
for (int i = 0; i < points.size(); i++) {
for (int j = i + 1; j < points.size(); j++) {
Distance distance = new Distance(getDistanceBetweenTwoPoints(points.get(i), points.get(j)), i, j);
distances.add(distance);
}
}
}
public void sortDistances() {
distances.sort((o1, o2) -> {
double distance1 = o1.getDistance();
double distance2 = o2.getDistance();
return Double.compare(distance1, distance2);
});
}
public double getDistanceBetweenTwoPoints(Point point1, Point point2) {
double differenceXCoordinates = point1.getX() - point2.getX();
double differenceYCoordinates = point1.getY() - point2.getY();
double xDifferenceSquared = differenceXCoordinates * differenceXCoordinates;
double yDifferenceSquared = differenceYCoordinates * differenceYCoordinates;
double distanceSquared = xDifferenceSquared + yDifferenceSquared;
return Math.sqrt(distanceSquared);
}
public void print() {
for (Group group : groups) {
List<String> labels = new ArrayList<>();
for (Point point : group.getPoints()) {
labels.add("" + point.getLabel());
}
String groupLabels = String.join(",", labels);
writer.println(groupLabels);
}
writer.flush();
}
public void cleanUp() {
try {
reader.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void initialise() {
initialiseReaders();
}
private void initialiseReaders() {
try {
reader = new BufferedReader(new FileReader(inputFilename));
writer = new PrintWriter(outputFilename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private class Point {
private double x;
private double y;
private Group group;
private int label;
private Point(double x, double y, int label) {
this.x = x;
this.y = y;
this.label = label;
}
public int getLabel() {
return label;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
private class Distance {
private double distance;
private int point1Index;
private int point2Index;
public Distance(double distance, int point1Index, int point2Index) {
this.distance = distance;
this.point1Index = point1Index;
this.point2Index = point2Index;
}
public double getDistance() {
return distance;
}
public int getPoint1Index() {
return point1Index;
}
public int getPoint2Index() {
return point2Index;
}
}
private class Group {
private List<Point> points = new ArrayList<>();
public Group(Point point) {
points.add(point);
}
public List<Point> getPoints() {
return points;
}
public void addAllPoints(Group group) {
points.addAll(group.points);
}
public void sort() {
points.sort(Comparator.comparingInt(Point::getLabel));
}
}
}