-
Notifications
You must be signed in to change notification settings - Fork 4
/
App.java
209 lines (190 loc) · 5.41 KB
/
App.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
// package track;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Queue;
import java.util.LinkedList;
import java.util.ArrayList;
public class App {
static int n, m;
static int[] di, dj;
static int min = Integer.MAX_VALUE;
static boolean[][] map;
public static void main(String[] args) {
// ---
// This is a sample code to use stdin and stdout.
// Edit and remove this code as you like.
String[] lines = getStdin();
String[] line = lines[0].split(" ");
n = toi(line[0]);
m = toi(line[1]);
di = new int[] { 1, 0, -1, 0 };
dj = new int[] { 0, 1, 0, -1 };
int time = 0;
//direction 0 : north, 1: east 2: south 3: west
map = new boolean[n][m];
for(boolean[] ar: map) Arrays.fill(ar, true);
Point hunter = new Point(0, 0, 0);
int[][] dis = new int[n][m];
for(int[] arr: dis) Arrays.fill(arr, -1);
ArrayList<Point> onis = new ArrayList<>();
for(int i = 1; i <= n; i++) {
for(int j = 0; j < m; j++) {
char ch = lines[i].charAt(j);
if(ch == '#') map[i - 1][j] = false;
else if(ch == 'H') {
hunter.i = i - 1;
hunter.j = j;
}
else if(ch == 'D') onis.add(new Point(i - 1, j, 1));
}
}
boolean[] visit = new boolean[onis.size()];
storeBfs(map, hunter, dis);
// println("map");
// for(int i = 0; i < n; i++) {
// for(int j = 0; j < m; j++) print(dis[i][j]+ " ");
// println("");
// }
for(Point oni: onis) {
if(dis[oni.i][oni.j] == -1) {
print(-1);
return;
}
if(oni.i == hunter.i && oni.j == hunter.j) {
print(0);
return;
}
}
while(true) {
for(Point oni: onis) {
int ii = oni.i, jj = oni.j, dir = oni.dir;
int toI = ii +di[dir], toJ = jj + dj[dir];
if(!properCoord(toI, toJ) || !map[toI][toJ]) {
oni.dir++;
if(oni.dir > 3) oni.dir -= 4;
} else {
oni.i = toI;
oni.j = toJ;
}
}
int minTime = Integer.MAX_VALUE;
boolean noZero = true;
for(int i = 0; i < onis.size(); i++) {
Point oni = onis.get(i);
if(dis[oni.i][oni.j] != 0) allZero = false;
else {
onis.remove(i);
if(onis.size() == 0) {
print(time + 1);
return;
}
time++;
continue;
}
}
for(int i = 0; i < onis.size(); i++) {
Point oni = onis.get(i);
ArrayList<Point> copy = new ArrayList<>();
for(int j = 0; j < onis.size(); j++) {
if(j == i) continue;
copy.add(onis.get(j));
}
min = Integer.MAX_VALUE;
recursive(copy, onis.get(i), 0);
// println(" min : " + min + " , " + "(" + oni.i + " , " + oni.j + " ) " + dis[oni.i][oni.j]);
if(min == Integer.MAX_VALUE) minTime = Math.min(minTime, dis[oni.i][oni.j]);
else minTime = Math.min(minTime, dis[oni.i][oni.j] + min);
}
time++;
println(time + " ,minTime: " + minTime);
for(Point oni: onis) {
println(oni.i + " : " + oni.j);
}
if(time >= minTime) {
print(time);
return;
}
}
}
static void recursive(ArrayList<Point> points, Point from, int dis) {
if(points.size() == 1) {
int distance = dis + bfs(from, points.get(0));
min = Math.min(distance, min);
return;
}
for(int i = 0; i < points.size(); i++) {
Point p = points.get(i);
points.remove(i);
recursive(points, p, dis + bfs(from, p));
points.add(i, p);
}
}
static int bfs(Point p1, Point p2) {
if(p1.i == p2.i && p1.j == p2.j) return 0;
int dis = 0;
boolean[][] visit = new boolean[n][m];
Queue<BFS> q = new LinkedList<>();
q.add(new BFS(p1.i, p1.j, 0));
while(!q.isEmpty()) {
BFS cur = q.poll();
if(visit[cur.i][cur.j]) continue;
visit[cur.i][cur.j] = true;
int ii = cur.i, jj = cur.j;
for(int i = 0; i < 4; i++ ) {
int I = ii + di[i], J = jj + dj[i];
if(!properCoord(I, J) || !map[I][J]) continue;
if(I == p2.i && J == p2.j) {
return cur.dis + 1;
}
q.add(new BFS(I, J, dis + 1));
}
}
return -1;
}
static void storeBfs(boolean[][] map, Point hunter, int[][] dis) {
boolean[][] visit = new boolean[n][m];
Queue<BFS> q = new LinkedList<>();
q.add(new BFS(hunter.i, hunter.j, 0));
while(!q.isEmpty()) {
BFS cur = q.poll();
if(visit[cur.i][cur.j]) continue;
visit[cur.i][cur.j] = true;
dis[cur.i][cur.j] = cur.dis;
int ii = cur.i, jj = cur.j;
for(int i = 0; i < 4; i++ ) {
int I = ii + di[i], J = jj + dj[i];
if(!properCoord(I, J) || !map[I][J]) continue;
q.add(new BFS(I, J, cur.dis + 1));
}
}
}
static boolean properCoord(int i, int j) {
if(i < 0 || j < 0 || i >= n || j >= m) return false;
return true;
}
static class Point {
int i, j, dir;
public Point(int i, int j, int dir) { this.i = i; this.j = j; this.dir = dir; }
}
static class BFS {
int i, j, dis;
public BFS(int i, int j, int dis) { this.i = i; this.j = j; this.dis = dis; }
}
static int toi(String s) { return Integer.parseInt(s); }
static <T> void print(T s) { System.out.print(s); }
static <T> void println(T s) { System.out.println(s); }
private static String[] getStdin() {
// Scanner scanner = new Scanner(System.in);
// ArrayList<String> lines = new ArrayList<>();
// while (scanner.hasNext()) {
// lines.add(scanner.nextLine());
// }
// return lines.toArray(new String[lines.size()]);
// }
ArrayList<String> lines = new ArrayList<>();
lines.add("2 2");
lines.add("D.");
lines.add("DH");
return lines.toArray(new String[lines.size()]);
}
}