-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcownav.java
294 lines (225 loc) · 6.91 KB
/
cownav.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
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Queue;
public class cownav {
static int N;
static boolean[][] maze;
static int[][][][][][] dp;
static int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // up, right, down, left
public static int rotate_left (int d) {
return d == 0 ? 3 : d - 1;
}
public static int rotate_right (int d) {
return (d + 1) % 4;
}
public static int[] move (int x, int y, int d) {
int[] pt = {x, y};
if (x == N - 1 && y == N - 1)
return pt;
if (d == 0)
pt[1] = Math.min (N - 1, y + 1);
else if (d == 1)
pt[0] = Math.min (N - 1, x + 1);
else if (d == 2)
pt[1] = Math.max (0, y - 1);
else
pt[0] = Math.max (0, x - 1);
if (!maze[pt[0]][pt[1]]) {
pt[0] = x;
pt[1] = y;
}
return pt;
}
public static int bfs () {
dp = new int[N][N][N][N][4][4];
for (int n = 0; n < N; ++n)
for (int n2 = 0; n2 < N; ++n2)
for (int n3 = 0; n3 < N; ++n3)
for (int n4 = 0; n4 < N; ++n4)
for (int n5 = 0; n5 < 4; ++n5)
for (int n6 = 0; n6 < 4; ++n6)
dp[n][n2][n3][n4][n5][n6] = 1 << 20;
dp[0][0][0][0][1][0] = dp[0][0][0][0][0][1] = 0;
State curr = new State (0, 0, 0, 0, 0, 1);
Queue<State> q = new ArrayDeque<State> ();
q.offer (curr);
int min = 1 << 20;
while (!q.isEmpty ()) {
curr = q.poll ();
//System.out.println (curr + " -> " + dp [curr.x1][curr.y1][curr.x2][curr.y2][curr.d1][curr.d2]);
if (curr.x1 == N - 1 && curr.y1 == N - 1 && curr.x2 == N - 1 && curr.y2 == N - 1) {
min = Math.min (min, dp[curr.x1][curr.y1][curr.x2][curr.y2][curr.d1][curr.d2]);
}
else {
// rotate left
if (dp[curr.x1][curr.y1][curr.x2][curr.y2][rotate_left (curr.d1)][rotate_left (curr.d2)] > dp[curr
.x1][curr.y1][curr.x2][curr.y2][curr.d1][curr.d2] + 1) {
dp[curr.x1][curr.y1][curr.x2][curr.y2][rotate_left (curr.d1)][rotate_left (curr.d2)] = dp[curr
.x1][curr.y1][curr.x2][curr.y2][curr.d1][curr.d2] + 1;
q.offer (new State (curr.x1, curr.y1, curr.x2, curr.y2, rotate_left (curr.d1), rotate_left (curr
.d2)));
}
// rotate right
if (dp[curr.x1][curr.y1][curr.x2][curr.y2][rotate_right (curr.d1)][rotate_right (curr.d2)] > dp[curr
.x1][curr.y1][curr.x2][curr.y2][curr.d1][curr.d2] + 1) {
dp[curr.x1][curr.y1][curr.x2][curr.y2][rotate_right (curr.d1)][rotate_right (curr.d2)] = dp[curr
.x1][curr.y1][curr.x2][curr.y2][curr.d1][curr.d2] + 1;
q.offer (new State (curr.x1, curr.y1, curr.x2, curr.y2, rotate_right (curr.d1), rotate_right (curr
.d2)));
}
// move forward
int[] move1 = move (curr.x1, curr.y1, curr.d1), move2 = move (curr.x2, curr.y2, curr.d2);
if (dp[move1[0]][move1[1]][move2[0]][move2[1]][curr.d1][curr.d2] > dp[curr.x1][curr.y1][curr.x2][curr
.y2][curr.d1][curr.d2] + 1) {
dp[move1[0]][move1[1]][move2[0]][move2[1]][curr.d1][curr.d2] = dp[curr.x1][curr.y1][curr.x2][curr
.y2][curr.d1][curr.d2] + 1;
q.offer (new State (move1[0], move1[1], move2[0], move2[1], curr.d1, curr.d2));
}
}
}
return min;
}
public static void main (String[] t) throws IOException {
INPUT in = new INPUT (System.in);
PrintWriter out = new PrintWriter (System.out);
N = in.iscan ();
maze = new boolean[N][N];
String ln;
boolean[][] maze2 = new boolean[N][N];
for (int n = 0; n < N; ++n) {
ln = in.sscan ();
for (int n2 = 0; n2 < N; ++n2)
maze2[n][n2] = ln.charAt (n2) == 'E';
}
// convert grid bc im lazy
for (int n = 0; n < N; ++n)
for (int n2 = 0; n2 < N; ++n2)
maze[n][n2] = maze2[N - n2 - 1][n];
/*for (int n = 0; n < N; ++n) {
for (int n2 = 0; n2 < N; ++n2)
out.print (maze [n][n2] ? "E" : "H");
out.println ();
}*/
out.print (bfs ());
out.close ();
}
private static class State {
int x1, y1, x2, y2, d1, d2;
public State (int x1, int y1, int x2, int y2, int d1, int d2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.d1 = d1;
this.d2 = d2;
}
}
private static class INPUT {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public INPUT (InputStream stream) {
this.stream = stream;
}
public INPUT (String file) throws IOException {
this.stream = new FileInputStream (file);
}
public static int fast_pow (int b, int x) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow (b * b, x / 2);
return b * fast_pow (b * b, x / 2);
}
public int cscan () throws IOException {
//if (numChars == -1) throw new InputMismatchException ();
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read (buf);
//if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public int iscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c)) c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
int res = 0;
do {
//if (c < '0' || c > '9') throw new InputMismatchException ();
res = (res << 1) + (res << 3);
//res *= 10;
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public String sscan () throws IOException {
int c = cscan ();
while (space (c)) c = cscan ();
StringBuilder res = new StringBuilder ();
do {
res.appendCodePoint (c);
c = cscan ();
}
while (!space (c));
return res.toString ();
}
public double dscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c)) c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
double res = 0;
while (!space (c) && c != '.') {
if (c == 'e' || c == 'E') return res * fast_pow (10, iscan ()); /*Math.pow (10, iscan ());*/
//if (c < '0' || c > '9') throw new InputMismatchException ();
//res = (res << 1) + (res << 3);
res *= 10;
res += c - '0';
c = cscan ();
}
if (c == '.') {
c = cscan ();
double m = 1;
while (!space (c)) {
if (c == 'e' || c == 'E') return res * fast_pow (10, iscan ()); /*Math.pow (10, iscan ());*/
//if (c < '0' || c > '9') throw new InputMismatchException ();
m /= 10;
res += (c - '0') * m;
c = cscan ();
}
}
return res * sgn;
}
public long lscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c)) c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
long res = 0;
do {
//if (c < '0' || c > '9') throw new InputMismatchException();
res = (res << 1) + (res << 3);
//res *= 10;
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public boolean space (int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}