-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMines.java
More file actions
394 lines (316 loc) · 13.9 KB
/
Mines.java
File metadata and controls
394 lines (316 loc) · 13.9 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package TreasureHunt;
// xrisimopoietai o algorithmos quickhull,
//Anastasios Pantzartzis ->apantzar@csd.auth.gr
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import static java.lang.System.out;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Mines {
/**
* This is the main class of my program. Totally I used 5 classes.
* Here i read the input from the txt and stored the points to the objects I Made.
* I have one class for the minefield which except of mines has the starting point and the treasure.
* The class named QuickHull is the only part of the programm that is not mine(i'll write the url there).
* One class for the second part of the project named treasure.
* Finally the class named Mine is the base class of my array lists.
*
* After storing the variables to my objects, main class is doesnt make many things.
*
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
final class Treasure {
private int dnum; //diamond number
private int w; //number of weightings
// o kataskeuastis tis klasis Treasure
public Treasure(){w=0;}
public void setDiaNum(int number){dnum=number;}
public int zygos(){
Random randomGenerator = new Random();
int x = randomGenerator.nextInt(100);
if (x<34)
return 1;
else if (x < 67)
return 0;
else
return -1;
}
// o algorithmos pou vriskei ta vari ton diadromon
// periptoseis me zugo kai peritto arithmo
public int weightings(){
if (dnum==1)
return w;
else if (dnum==2)
return w+1;
else{
w+=1;
dnum=dnum/3;
if (dnum%3!=0){
int mod=dnum%3;
int z=zygos();
if (z==0)
dnum+= mod;
}
//weightings();
}
return w;
}
}
// klasi Mine, xrisompoieitai gia na lambvanei tis syntetagmenew tis narkis
final class Mine {
private int x;
private int y;
/**
* Constructor.
* @param x
* @param y
*/
public Mine(int x,int y){
this.x=x;
this.y=y;
}
/**
* getters
* @return
*/
public int getX(){return x;}
public int getY(){return y;}
}
//klasi Minefield, xrisompoieitai gia na kataxorei se disdistato pinaka tis
//syntatagmenew pou tha diavazontai sto arxeio txt, diladi ta empodia apo ta opoia den mporei na
//perasei kai yparxoyns oi narkes, diladi ta mines
final class MineField {
private ArrayList<Mine> Mines, path1, path2;
/**
* Constructor
*/
public MineField(){
Mines= new ArrayList<>();
path1= new ArrayList<>();
path2= new ArrayList<>();
}
// prosthiki sti mines Apath1 and path 2 a Mine.
public void add(Mine mine){Mines.add(mine);}
public void add1(Mine mine){path1.add(mine);}
public void add2(Mine mine){path2.add(mine);}
public ArrayList<Mine> getMines(){return Mines;}
public ArrayList<Mine> getpath1(){return path1;}
public ArrayList<Mine> getpath2(){return path2;}
// parakato ginetai h eurersi ton diadromon me ton quickhull
// xrismopoiountai 3 epanalipseis gia na vrethoun oi diadromes
//h metavliti ekk einai gia tin ekkinisi
// i metavliti tr einai to simeio sto opoio vrisketai o thisauros
public void paths(int ekk ,int tr,ArrayList<Mine> hull){
for(int i=ekk; i<=tr;i++){
path1.add(hull.get(i)); //path1
}
for(int i=tr; i<hull.size();i++){ //misi diadromi path2
path2.add(hull.get(i));
}
for(int i=0; i<=ekk;i++){ //alli misi diadromi path2
path2.add(hull.get(i));
}
Collections.reverse(path2);
}
// parakato ypologizetai to kostos tis diadromis
//i metavliti i1 kai i2 einai ta simeia, enoi stin metavliti sum apothikeueta to athroisma
// tou kostoyw ton simeion
public double pathDistance(ArrayList<Mine> path){
double sum=0;
int i1=0,i2=1;
while (i2<path.size()){
sum+=calcDistance(path.get(i1).getX(),path.get(i1).getY(),path.get(i2).getX(),path.get(i2).getY());
i1++;
i2++;
}
return sum;
}
// ypologismos kostoys symfona me ton typo tis eukleidias apostasis
public double calcDistance(int x1, int y1, int x2 ,int y2) {
return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
// kai telika briskoume tin pio suntomi diadromi
public void toStr(ArrayList<Mine> path){
System.out.print("The shortest path is:");
for(int i=0;i<path.size();i++){
Mine m=path.get(i);
System.out.print("("+(float)m.getX()+","+(float)m.getY()+")");
if(i!=path.size()-1){System.out.print("-->");}
}
System.out.println();
}
}
//klasi QuckHull, gia tin efarmogi tou algorithou gia tin euresu tis pio syntomis diadromis
final class QuickHull {
public ArrayList<Mine> quickHull(ArrayList<Mine> points)
{
ArrayList<Mine> convexHull = new ArrayList<Mine>();
if (points.size() < 3)
return (ArrayList) points.clone();
int minPoint = -1, maxPoint = -1;
int minX = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;
for (int i = 0; i < points.size(); i++)
{
if (points.get(i).getX() < minX)
{ //vriskoume elaxisto kai megisto
minX = points.get(i).getX();
minPoint = i;
}
if (points.get(i).getX() > maxX)
{
maxX = points.get(i).getX();
maxPoint = i;
}
}
Mine A = points.get(minPoint);
Mine B = points.get(maxPoint);
convexHull.add(A); // prosthetoume ston hull kai afairoume apo ta simeia
convexHull.add(B);
points.remove(A);
points.remove(B);
ArrayList<Mine> leftSet = new ArrayList<Mine>();
ArrayList<Mine> rightSet = new ArrayList<Mine>();
for (int i = 0; i < points.size(); i++)
{
Mine p = points.get(i);
if (pointLocation(A, B, p) == -1)
leftSet.add(p);
else if (pointLocation(A, B, p) == 1)
rightSet.add(p);
}
hullSet(A, B, rightSet, convexHull);
hullSet(B, A, leftSet, convexHull);
return convexHull;
}
// ypologismos diadromis
public int distance(Mine A, Mine B, Mine C)
{
int ABx = B.getX() - A.getX();
int ABy = B.getY() - A.getY();
int num = ABx * (A.getY() - C.getY()) - ABy * (A.getX() - C.getX());
if (num < 0)
num = -num;
return num;
}
public void hullSet(Mine A, Mine B, ArrayList<Mine> set,
ArrayList<Mine> hull)
{
int insertPosition = hull.indexOf(B);
if (set.size() == 0)
return;
if (set.size() == 1)
{
Mine p = set.get(0);
set.remove(p);
hull.add(insertPosition, p);
return;
}
int dist = Integer.MIN_VALUE;
int furthestPoint = -1;
for (int i = 0; i < set.size(); i++)
{
Mine p = set.get(i);
int distance = distance(A, B, p); //euresi tou pio suntomou simeiou
if (distance > dist)
{
dist = distance;
furthestPoint = i;
}
}
Mine P = set.get(furthestPoint);
set.remove(furthestPoint);
hull.add(insertPosition, P);
// kauoristmos poio einai aristera toy AP
ArrayList<Mine> leftSetAP = new ArrayList<Mine>();
for (int i = 0; i < set.size(); i++)
{
Mine M = set.get(i);
if (pointLocation(A, P, M) == 1)
{
leftSetAP.add(M);
}
}
// kauoristmos poio einai aristera toy PB
ArrayList<Mine> leftSetPB = new ArrayList<Mine>();
for (int i = 0; i < set.size(); i++)
{
Mine M = set.get(i);
if (pointLocation(P, B, M) == 1)
{
leftSetPB.add(M);
}
}
hullSet(A, P, leftSetAP, hull);
hullSet(P, B, leftSetPB, hull);
}
//euresi an kapoio simeio anikei sto aristero h deksio synolo
public int pointLocation(Mine A, Mine B, Mine P)
{
int cp1 = (B.getX() - A.getX()) * (P.getY() - A.getY()) - (B.getY() - A.getY()) * (P.getX() - A.getX());
if (cp1 > 0)
return 1;
else if (cp1 == 0)
return 0;
else
return -1;
}
}
int xekk=0,yekk=0,xtr=0,ytr=0;
MineField mf=new MineField();
//to Minefied apothikeuei simeio ekkinisis, simeio pou vrisketai o thisauros kai syntetagmenew apo ta mines
Treasure treasure=new Treasure();
try (BufferedReader in = new BufferedReader( new FileReader("c:/data.txt"));)
{
String l;
int line=1;
while ((l = in.readLine()) != null) //Diavasma arxeiou txt gia tis syntetagmenes
{
//Diaxorismos kathe grammis gia na pairnoume syntetagmenew x kai y gia ta mines ktl.
String[] l2=l.split(" ");
int x=Integer.parseInt(l2[0]) , y=Integer.parseInt(l2[1]);
Mine mine=new Mine(x,y);
mf.add(mine);
if(line==1){
//H grammi ena kseroume oti einai oi syntentagmenes x kai y tou simmeiou ekkinisis
xekk=mine.getX();
yekk=mine.getY();
}
if(line==2){
xtr=mine.getX();
ytr=mine.getY();
}
line++;
}
}
//klisi tou quickhull's kai apothikeusi se lista pinaka me onoma p
QuickHull qh = new QuickHull();
ArrayList<Mine> p = qh.quickHull(mf.getMines());
//simeio ekkinisis kai simeio pou vrisketai o thisauros
int ekk=0,tr=0;
for (int i=0;i<p.size();i++){
if(p.get(i).getX()==xekk && p.get(i).getY()==yekk){ekk=i;}
if(p.get(i).getX()==xtr && p.get(i).getY()==ytr){tr=i;}
}
mf.paths(ekk, tr, p);
//stelno tiw 2 diaforetikes diadromes pou odigoun sto thisauro
if(mf.pathDistance(mf.getpath1())< mf.pathDistance(mf.getpath2())){
//System.out.println("The shortest distance is "+mf.pathDistance(mf.getpath1()));
System.out.println("The shortest distance is");
System.out.format ("%.5f",mf.pathDistance(mf.getpath1()));
System.out.println("/n");
//kalo tin sunartisi gia tin euresi tis pio syntomis diadromis kai tin ektypoynoume stin othoni
mf.toStr(mf.getpath1());
}
else {
System.out.println("The shortest distance is");
System.out.format ("%.5f",mf.pathDistance(mf.getpath2()));
mf.toStr(mf.getpath2());
}
}
}