-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApproach2.java
More file actions
301 lines (267 loc) · 7.94 KB
/
Approach2.java
File metadata and controls
301 lines (267 loc) · 7.94 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.Vector;
class Approach2 {
static int numOfZones;
static String departureAirport;
static String currentAirport;
static int totalPrice = 0;
static String curLine="";
static Boolean [] zones;
static String [] allZones;
static Vector<String> allCitys = new Vector<String>();
static String leaving;
static String going;
static int date;
static int cost;
static int[][][] flightCost;
static int areaCount=-1;
static Vector<Integer> citysArea = new Vector<Integer>();
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
File file = new File("C:\\Users\\Kkdj2\\Desktop\\algs\\10_Nodes_5_Ports.txt");
@SuppressWarnings("resource")
Scanner sc = new Scanner("it");
try {
sc = new Scanner(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
numOfZones = sc.nextInt();
/*
* phase one is the first line in the file it has the how many zones there are
* and what is the starting area
*/
allZones = new String [numOfZones];
zones = new Boolean [numOfZones];
departureAirport = sc.next();
sc.nextLine();
/*
* allZones is set to the length of numOfZones then allZones is filled with null so
* we can add the zone names in later.
*/
for(int i =0; i<numOfZones;i++)
{
allZones[i]=null;
}
for(int i =0; i<numOfZones;i++)
{
zones[i]=false;
}
/*
* phase two starts after the phase one end. phase two then goes into
* adding names for the zones and then what city's are in the zone
*/
for(int i =0; i<numOfZones*2;i++)
{
/*
* the line alternate between zone then city's so when if i%2==0 then we know that
* it is a zone when i is equal to 1 then we know it is a city
*/
if(i%2==0)
{
/*
* adds the zone into an empty space zone
* and increase the areaCount so we can keep track of which cities are in
* what area
*/
allZones[findZoneNull()]=sc.nextLine();
areaCount++;
}
else {
/*
* curLine is equal the city line it checks to see how many cities are there
* based on count_Words it does that by looking for spaces in the string
*/
curLine=sc.nextLine();
int x = count_Words(curLine);
/*
* if there are more then one word in the string then we split the string
* by how many spaces are in the string then we add them to the allCitys vector
* we also add what zone the city in by the citysArea vector
*/
if (x!=1)
{
String[] split= curLine.split("\\s+");
for(int j =0; j<split.length;j++)
{
allCitys.add(split[j]);
citysArea.add(areaCount);
}
}
/*
* if there is only one word in the string we just add the whole string to the
* vector
*/
else
{
allCitys.add(curLine);
citysArea.add(areaCount);
}
}
}
/*
* flightCost is where we store what flights cost
* the first [] is the date of which the plane is leaving
* the second [] is what city the plane is leaving from
* the third [] is what city the plane is going to
*/
flightCost= new int [numOfZones+1][allCitys.size()][allCitys.size()];
/*
* phase three starts after phase two. phase three is getting the cost of the flights
* the pattern goes string string int int
* first string is the where it is leaving from
* second string is where it is going
* first int is what day
* second int is the cost
*/
while(true)
{
/*
* the while loop will keep going until there is nothing left in the file
* to find that we first try sc.next if there is something there then we know
* its a line of data if it fails then we know that the file is done
*/
try {
leaving=sc.next();
}
catch(NoSuchElementException e)
{
break;
}
going=sc.next();
date=sc.nextInt();
cost=sc.nextInt();
flightCost[date][allCitys.indexOf(leaving)][allCitys.indexOf(going)]=cost;
}
/*
System.out.println("all of the zones");
for(int i =0; i<numOfZones;i++)
{
System.out.println(allZones[i]);
}
System.out.println("all of the citys");
for(int i =0; i<allCitys.size();i++)
{
System.out.println(allCitys.elementAt(i));
}
System.out.println("what city in what zone");
for(int i =0; i<citysArea.size();i++)
{
System.out.println(citysArea.elementAt(i));
}
System.out.println("what the flights cost looks like");
for(int i =0; i<numOfZones;i++)
{
for(int j =0; j<allCitys.size();j++)
for(int k =0; k<allCitys.size();k++)
{
if(flightCost[i][j][k] != 0) { // All flights has cost
System.out.println(flightCost[i][j][k] + " " + allCitys.elementAt(j) + " " + allCitys.elementAt(k));
}
}
}
*/
System.out.println("An optimal route is: ");
System.out.println(findBestRoute());
System.out.println("The total price for this route is: ");
System.out.println(totalPrice);
long endTime = System.currentTimeMillis();
System.out.println("Finding this route took: "+(endTime - startTime) + " milliseconds");
}
/*
* this looks to see how many words are in the string
* idk the code since it was riped from a website
*/
public static int count_Words(String str)
{
int count = 0;
try {
if(!(" ".equals(str.substring(0, 1))) || !(" ".equals(str.substring(str.length() - 1))))
{
}
}
catch(StringIndexOutOfBoundsException e)
{
return 1;
}
if (!(" ".equals(str.substring(0, 1))) || !(" ".equals(str.substring(str.length() - 1))))
{
for (int i = 0; i < str.length(); i++)
{
if (str.charAt(i) == ' ')
{
count++;
}
}
count = count + 1;
}
return count; // returns 0 if string starts or ends with space " ".
}
/*
* find where the next null is and returns it to the zone.
*/
public static int findZoneNull()
{
for(int i =0; i<=numOfZones;i++)
{
if(allZones[i]==null)
{
return i;
}
}
return-1;
}
public static Vector<String> findBestRoute()
{
Vector<String> bestRoute = new Vector<String>();
zones[citysArea.elementAt(allCitys.indexOf(departureAirport))] = true;
bestRoute.addElement(departureAirport);
int day = 1;
int currentCost = 1000000;
String currentCity = departureAirport;
String bestDestination = null;
for(int i = 1; i<numOfZones;i++)
{
if(day == i)
{
for(int j = 0; j<allCitys.size();j++)
{
if(currentCity != null)
{
if(currentCity.equals(allCitys.elementAt(j)))
{
for(int k= 0; k<allCitys.size();k++)
{
if(flightCost[i][j][k] != 0 && !bestRoute.contains(allCitys.elementAt(k)) && !zones[citysArea.elementAt(k)]) {// All flights has cost
if(flightCost[i][j][k] < currentCost)
{
currentCost = flightCost[i][j][k];
bestDestination = allCitys.elementAt(k);
}
}
}
currentCity = bestDestination;
if(bestDestination != null )
{
bestRoute.addElement(bestDestination);
zones[citysArea.elementAt(allCitys.indexOf(bestDestination))] = true;
bestDestination = null;
totalPrice += currentCost;
currentCost = 1000000;
break;
}
}
}
}
}
day++;
}
totalPrice += flightCost[numOfZones][allCitys.indexOf(currentCity)][allCitys.indexOf(departureAirport)];
bestRoute.addElement(departureAirport);
return bestRoute;
}
}