-
Notifications
You must be signed in to change notification settings - Fork 0
/
challange3.java
159 lines (144 loc) · 5.66 KB
/
challange3.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
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class challange3 {
public static void BellmanFord(City[][] map, int n, int col, int row) {
List<tuple> edges = getEdges(n);
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i == col && j == row) map[i][j].dist = 0;
else map[i][j].dist = 1000000;
}
}
for(int i = 0; i < n*n; i++) {
for(tuple t : edges) {
if(map[t.dest[0]][t.dest[1]].dist > map[t.source[0]][t.source[1]].dist + map[t.dest[0]][t.dest[1]].weight) {
map[t.dest[0]][t.dest[1]].dist = map[t.source[0]][t.source[1]].dist + map[t.dest[0]][t.dest[1]].weight;
map[t.dest[0]][t.dest[1]].pred = t;
}
}
}
}
public static void printPath(City[][] map, int s1, int s2, int d1, int d2) {
boolean term = true;
while(term) {
System.out.print("(" + String.valueOf(d1) + ", " + String.valueOf(d2) + ")");
if(d1 == s1 && d2 == s2) {
System.out.println();
term = false;
continue;
}
System.out.print(", ");
int temp1 = map[d1][d2].pred.source[0];
int temp2 = map[d1][d2].pred.source[1];
d1 = temp1; d2 = temp2;
}
}
public static void updateNeedArray(City[][] map, int s1, int s2, int d1,int d2) {
for(int i = 0; i < 6; i++) {
if(map[s1][s2].have[i] == 1) map[d1][d2].need[i] = 0;
if(map[d1][d2].have[i] == 1) map[s1][s2].need[i] = 0;
}
}
public static void getShortest(City[][] map, int n, int col, int row, int k) {
int min = Integer.MAX_VALUE, coor1 = -1, coor2 = -1;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(map[i][j].have[k] == 1 && map[i][j].dist < min) {
min = map[i][j].dist;
coor1 = i; coor2 = j;
}
}
}
if(coor1 == -1) return;
printPath(map, col, row, coor1, coor2);
updateNeedArray(map, col, row, coor1, coor2);
}
public static void handleCity(City[][] map, String line) {
String[] temp = line.split("\\(")[1].split("\\)");
String[] coor = temp[0].split(", ");
String[] resources = temp[1].split(", ");
resources = Arrays.copyOfRange(resources, 1, resources.length);
map[Integer.valueOf(coor[0])][Integer.valueOf(coor[1])].findHaveArray(resources);
map[Integer.valueOf(coor[0])][Integer.valueOf(coor[1])].haveCity = true;
}
static int handleTerrain(City[][] map, int n, int i, String line) {
String[] terrain = line.split("\\[")[1].split("],")[0].split(", ");
for(int j = 0; j < n; j++) {
map[i][j].findWeight(terrain[j]);
}
return 1;
}
public static void initializeMap(City[][] map, int n) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
map[i][j] = new City();
}
}
}
public static City[][] getMap(String fileName, int n) throws Exception{
City[][] map = new City[n][n];
initializeMap(map, n);
File file = new File(fileName);
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
boolean flag = true;
int i = 0;
while((line = br.readLine()) != null) {
if(line.equals("cities")) flag = false;
else if(flag) i += handleTerrain(map, n, i, line);
else handleCity(map, line);
}
br.close();
return map;
}
public static void getEdgeEven(List<tuple> res, int i, int j) {
res.add(new tuple(i, j, i, j-1));
res.add(new tuple(i, j, i-1,j-1));
res.add(new tuple(i, j, i-1,j));
res.add(new tuple(i, j, i, j+1));
res.add(new tuple(i, j, i+1,j));
res.add(new tuple(i, j, i+1,j-1));
}
public static void getEdgeOdd(List<tuple> res, int i, int j) {
res.add(new tuple(i, j, i, j - 1));
res.add(new tuple(i, j, i - 1, j));
res.add(new tuple(i, j, i - 1, j + 1));
res.add(new tuple(i, j, i, j + 1));
res.add(new tuple(i, j, i + 1, j + 1));
res.add(new tuple(i, j, i + 1, j));
}
public static List<tuple> getEdges(int n) {
List<tuple> temp = new ArrayList<>();
List<tuple> res = new ArrayList<>();
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i % 2 == 0) getEdgeEven(temp, i, j);
else getEdgeOdd(temp, i, j);
}
}
for(tuple t : temp) {
if(!(t.dest[0] < 0 || t.dest[1] < 0 || t.dest[0] > n-1 || t.dest[1] > n-1)) {
res.add(t);
res.add(new tuple(t.dest[0], t.dest[1], t.source[0], t.source[1]));
}
}
return res;
}
public static void main(String[] args) throws Exception{
final int n = 64;
String fileName = "input.txt";
City[][] map = getMap(fileName, n);
for(int k = 0; k < 6; k++) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(map[i][j].haveCity && map[i][j].need[k] == 1) {
BellmanFord(map, n, i, j);
getShortest(map, n, i, j, k);
}
}
}
}
}
}