-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
315 lines (250 loc) · 9.49 KB
/
Main.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
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
import java.io.*;
import java.util.*;
public class Main {
static int R;
static int C;
static int nSnakes; // S
static int[] lengthSnakes;
static int[][] system; // wormhole = -10,001
static boolean[][] occupied;
static int numWormHoles = 0;
static int[][] wormHoles;
static String[] paths;
static int possibleScore = 0;
public static void main(String args[]) { // static?
try {
String inputFile = args[0];
readFile(inputFile);
int score = 0;
for (int i = 0; i < nSnakes; i++) {
int[] coords = findMaxXY();
Path snakePath = createSnake(lengthSnakes[i], coords[0], coords[1], coords[0] + " " + coords[1] + " ", 0, i); // create initial snake
paths[i] = snakePath.getPath();
setOccupied(snakePath.getPath());
score += snakePath.getScore();
System.out.println(String.format("Snake Number: %d, Score: %d", i, snakePath.getScore()));
}
for (int i = 0; i < nSnakes; i++) { // output
System.out.println(paths[i]);
}
// for testing purposes:
System.out.println("Total Score: " + score + " / " + possibleScore); // TODO: comment out at the end
} catch (RuntimeException e) {
System.out.println("Usage: java Main input_file.txt");
e.printStackTrace();
return;
} catch (FileNotFoundException e) {
System.out.println("File Not Found Exception");
e.printStackTrace();
return;
} catch (Exception e) {
e.printStackTrace();
return;
}
}
public static void readFile(String filename) throws Exception {
try {
File f = new File(filename);
FileInputStream fileStream = new FileInputStream(f);
Scanner scan = new Scanner(fileStream);
// first input line - read the input values for system
C = scan.nextInt();
R = scan.nextInt();
nSnakes = scan.nextInt();
paths = new String[nSnakes];
scan.nextLine(); // clear white space
lengthSnakes = new int[nSnakes];
occupied = new boolean[R][C];
system = new int[R][C];
wormHoles = new int[R*C][2];
// second input line - snake lengths
for (int i = 0; i < nSnakes; i++) {
lengthSnakes[i] = scan.nextInt();
}
scan.nextLine(); // clear white space
// rest of input - make the system
for (int row = 0; row < R; row++) { // for each row
for (int col = 0; col < C; col++) {
String input = scan.next();
if (input.equals("*")) { // wormhole
system[row][col] = -10001;
wormHoles[numWormHoles] = new int[]{row, col};
numWormHoles++;
} else { // int value (points)
system[row][col] = Integer.parseInt(input);
possibleScore += Integer.parseInt(input);
}
occupied[row][col] = false;
}
// scan.nextLine();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* Recursive function made to create a snake
* Starts at the highest value available
*/
// TODO: Defaults to up on last because return 0 score
public static Path createSnake(int length, int x, int y, String path, int score, int index) {
// Any value of -10001 is a wormhole
if (length <= 0) {
Path path_result = new Path(score, path.substring(0, path.length() - 3));
return path_result;
}
// Adjust x and y if needed
if (x < 0) {
x += C;
}
if (x >= C) {
x -= C;
}
if (y < 0) {
y += R;
}
if (y >= R) {
y -= R;
}
// Penalize biggest score if path is blocked
if (occupied[y][x]) {
return new Path(score + (length * -10001), path);
} else {
Path[] list_paths = new Path[4];
// Path array goes U L D R
if (system[y][x] == -10001) { // wormhole - still counts as a move
//length--; // Travel to the wormhole
// TODO: go to each wormhole?
for (int i = 0; i < numWormHoles; i++) {
int newY = wormHoles[i][0];
int newX = wormHoles[i][1];
if (x != newX && y != newY) {
path += newX + " " + newY + " ";
x = newX;
y = newY;
list_paths[0] = createSnake(length - 1, x, y - 1, path + "U ", score, index);
list_paths[1] = createSnake(length - 1, x - 1, y, path + "L ", score, index);
list_paths[2] = createSnake(length - 1, x, y + 1, path + "D ", score, index);
list_paths[3] = createSnake(length - 1, x + 1, y, path + "R ", score, index);
// Find the path with the biggest score
int[] list_scores = { list_paths[0].getScore(), list_paths[1].getScore(), list_paths[2].getScore(), list_paths[3].getScore() };
int score_index = 0;
int maxScore = list_scores[score_index];
for (int j = 0; j < list_scores.length; j++) {
if (list_scores[j] > maxScore) {
maxScore = list_scores[j];
score_index = j;
}
}
return list_paths[score_index];
}
}
} else { // value
score += system[y][x];
occupied[y][x] = true;
list_paths[0] = createSnake(length - 1, x, y - 1, path + "U ", score, index);
list_paths[1] = createSnake(length - 1, x - 1, y, path + "L ", score, index);
list_paths[2] = createSnake(length - 1, x, y + 1, path + "D ", score, index);
list_paths[3] = createSnake(length - 1, x + 1, y, path + "R ", score, index);
occupied[y][x] = false;
// Find the path with the biggest score
int[] list_scores = { list_paths[0].getScore(), list_paths[1].getScore(), list_paths[2].getScore(), list_paths[3].getScore() };
int score_index = 0;
int maxScore = list_scores[score_index];
for (int j = 0; j < list_scores.length; j++) {
if (list_scores[j] > maxScore) {
maxScore = list_scores[j];
score_index = j;
}
}
return list_paths[score_index];
}
}
return null;
}
public static void setOccupied(String path) {
Scanner scan = new Scanner(path);
int x = scan.nextInt();
int y = scan.nextInt();
occupied[y][x] = true;
String input;
while (scan.hasNext()) {
input = scan.next();
switch (input) {
case("L"):
x -= 1;
if (x < 0) {
x += C;
}
occupied[y][x] = true;
break;
case("D"):
y += 1;
if (y >= R) {
y -= R;
}
occupied[y][x] = true;
break;
case("R"):
x += 1;
if (x >= C) {
x -= C;
}
occupied[y][x] = true;
break;
case("U"):
y -= 1;
if (y < 0) {
y += R;
}
occupied[y][x] = true;
break;
default:
x = Integer.parseInt(input);
y = scan.nextInt();
}
}
}
/*
* Returns the index of the max value found in system
*/
public static int[] findMaxXY() {
// Find index of max non-taken value
int maxX = 0, maxY = 0;
int maxValue = -10000;
for (int x = 0; x < C; x++){
for (int y = 0; y < R; y++) {
// If square is not occupied and is bigger than given maxValue
if (!occupied[y][x] && (system[y][x] > maxValue)) {
maxX = x;
maxY = y;
maxValue = system[y][x];
}
}
}
int[] indexes = new int[2];
indexes[0] = maxX;
indexes[1] = maxY;
return indexes;
}
}
class Path {
public int score;
public String path;
public Path(int score, String path) {
this.score = score;
this.path = path;
}
public int getScore() {
return score;
}
public void setScore(int newScore) {
this.score = newScore;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}