-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13460.java
346 lines (273 loc) · 9.81 KB
/
13460.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import java.util.*;
class Point {
int x;
int y;
Point(int x, int y) {this.x=x; this.y=y;}
}
public class Main {
public static int answer = Integer.MAX_VALUE;
public static char[][] copy(char[][] map_origin) {
int n = map_origin.length;
int m = map_origin[0].length;
char[][] map = new char[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
map[i][j] = map_origin[i][j];
}
}
return map;
}
public static boolean up(char[][] map, int moved, Point red, Point blue) {
int i;
if(red.y == blue.y && blue.x < red.x) { // blue부터 진행해야만 하는 경우
for(i=blue.x-1; i>=0 && map[i][blue.y] == '.'; i--);
if(map[i][blue.y] == 'O') // blue가 빠져서 실패
return true;
else { // 이제 '#' 인 경우만 존재
map[blue.x][blue.y] = '.';
map[i+1][blue.y] = 'B';
blue.x = i+1;
}
for(i=red.x-1; i>=0 && map[i][red.y] == '.'; i--);
if(map[i][red.y] == 'O') { // red가 빠진 경우 성공
answer = Integer.min(answer, moved);
return true;
}
else { // '#' 이거나 'B'인 경우
map[red.x][red.y] = '.';
map[i+1][red.y] = 'R';
red.x = i+1;
}
}
else { // red부터 진행
boolean redIn = false;
for(i=red.x-1; i>=0 && map[i][red.y] == '.'; i--);
if(map[i][red.y] == 'O') { // red가 빠진 경우 성공인데 뒤에 blue까지 봐야함
redIn = true;
map[red.x][red.y] = '.';
}
else { // '#' 인 경우만 존재
map[red.x][red.y] = '.';
map[i+1][red.y] = 'R';
red.x = i+1;
}
for(i=blue.x-1; i>=0 && map[i][blue.y] == '.'; i--);
if(map[i][blue.y] == 'O') // blue가 빠져서 실패
return true;
else { // '#'이거나 'R'인 경우
map[blue.x][blue.y] = '.';
map[i+1][blue.y] = 'B';
blue.x = i+1;
}
// blue에서 빠지지 않은 경우 red가 빠졌으므로 답임
if(redIn) {
answer = Integer.min(answer, moved);
return true;
}
}
return false;
}
public static boolean down(char[][] map, int moved, Point red, Point blue) {
int i;
if(red.y == blue.y && red.x < blue.x) { // blue부터 진행해야만 하는 경우
for(i=blue.x+1; i<map.length && map[i][blue.y] == '.'; i++);
if(map[i][blue.y] == 'O') // blue가 빠져서 실패
return true;
else { // 이제 '#' 인 경우만 존재
map[blue.x][blue.y] = '.';
map[i-1][blue.y] = 'B';
blue.x = i-1;
}
for(i=red.x+1; i<map.length && map[i][red.y] == '.'; i++);
if(map[i][red.y] == 'O') { // red가 빠진 경우 성공
answer = Integer.min(answer, moved);
return true;
}
else { // '#' 이거나 'B'인 경우
map[red.x][red.y] = '.';
map[i-1][red.y] = 'R';
red.x = i-1;
}
}
else { // red부터 진행
boolean redIn = false;
for(i=red.x+1; i<map.length && map[i][red.y] == '.'; i++);
if(map[i][red.y] == 'O') { // red가 빠진 경우
map[red.x][red.y] = '.';
redIn = true;
}
else { // '#' 이거나 'B'인 경우
map[red.x][red.y] = '.';
map[i-1][red.y] = 'R';
red.x = i-1;
}
for(i=blue.x+1; i<map.length && map[i][blue.y] == '.'; i++);
if(map[i][blue.y] == 'O') // blue가 빠져서 실패
return true;
else { // 이제 '#' 인 경우만 존재
map[blue.x][blue.y] = '.';
map[i-1][blue.y] = 'B';
blue.x = i-1;
}
// blue에서 빠지지 않은 경우 red가 빠졌으므로 답임
if(redIn) {
answer = Integer.min(answer, moved);
return true;
}
}
return false;
}
public static boolean left(char[][] map, int moved, Point red, Point blue) {
int i;
if(red.x == blue.x && blue.y < red.y) { // blue부터 진행해야만 하는 경우
for(i=blue.y-1; i>=0 && map[blue.x][i] == '.'; i--);
if(map[blue.x][i] == 'O') // blue가 빠져서 실패
return true;
else { // 이제 '#' 인 경우만 존재
map[blue.x][blue.y] = '.';
map[blue.x][i+1] = 'B';
blue.y = i+1;
}
for(i=red.y-1; i>=0 && map[red.x][i] == '.'; i--);
if(map[red.x][i] == 'O') { // red가 빠진 경우 성공
answer = Integer.min(answer, moved);
return true;
}
else { // '#' 이거나 'B'인 경우
map[red.x][red.y] = '.';
map[red.x][i+1] = 'R';
red.y = i+1;
}
}
else { // red부터 진행
boolean redIn = false;
for(i=red.y-1; i>=0 && map[red.x][i] == '.'; i--);
if(map[red.x][i] == 'O') { // red가 빠진 경우
map[red.x][red.y] = '.';
redIn = true;
}
else { // '#' 이거나 'B'인 경우
map[red.x][red.y] = '.';
map[red.x][i+1] = 'R';
red.y = i+1;
}
for(i=blue.y-1; i>=0 && map[blue.x][i] == '.'; i--);
if(map[blue.x][i] == 'O') // blue가 빠져서 실패
return true;
else { // 이제 '#' 인 경우만 존재
map[blue.x][blue.y] = '.';
map[blue.x][i+1] = 'B';
blue.y = i+1;
}
// blue에서 빠지지 않은 경우 red가 빠졌으므로 답임
if(redIn) {
answer = Integer.min(answer, moved);
return true;
}
}
return false;
}
public static boolean right(char[][] map, int moved, Point red, Point blue) {
int i;
if(red.x == blue.x && red.y < blue.y) { // blue부터 진행해야만 하는 경우
for(i=blue.y+1; i<map[0].length && map[blue.x][i] == '.'; i++);
if(map[blue.x][i] == 'O') // blue가 빠져서 실패
return true;
else { // 이제 '#' 인 경우만 존재
map[blue.x][blue.y] = '.';
map[blue.x][i-1] = 'B';
blue.y = i-1;
}
for(i=red.y+1; i<map[0].length && map[red.x][i] == '.'; i++);
if(map[red.x][i] == 'O') { // red가 빠진 경우 성공
answer = Integer.min(answer, moved);
return true;
}
else { // '#' 이거나 'B'인 경우
map[red.x][red.y] = '.';
map[red.x][i-1] = 'R';
red.y = i-1;
}
}
else { // red부터 진행
boolean redIn = false;
for(i=red.y+1; i<map[0].length && map[red.x][i] == '.'; i++);
if(map[red.x][i] == 'O') { // red가 빠진 경우
map[red.x][red.y] = '.';
redIn = true;
}
else { // '#' 이거나 'B'인 경우
map[red.x][red.y] = '.';
map[red.x][i-1] = 'R';
red.y = i-1;
}
for(i=blue.y+1; i<map[0].length && map[blue.x][i] == '.'; i++);
if(map[blue.x][i] == 'O') // blue가 빠져서 실패
return true;
else { // 이제 '#' 인 경우만 존재
map[blue.x][blue.y] = '.';
map[blue.x][i-1] = 'B';
blue.y = i-1;
}
// blue에서 빠지지 않은 경우 red가 빠졌으므로 답임
if(redIn) {
answer = Integer.min(answer, moved);
return true;
}
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
sc.nextLine();
char[][] map = new char[n][m];
Point red = null;
Point blue = null;
for(int i=0; i<n; i++) {
String str = sc.nextLine();
for(int j=0; j<m; j++) {
char c = str.charAt(j);
if(c == 'R')
red = new Point(i, j);
else if(c == 'B')
blue = new Point(i, j);
map[i][j] = c;
}
}
// 비트마스크로 브루트 포스
for(int bit=(1 << 20)-1; bit>=0; bit--) {
// 한 가지 케이스 시작
int tc = bit;
// 맵 복사
char[][] mapNew = copy(map);
Point redNew = new Point(red.x, red.y);
Point blueNew = new Point(blue.x, blue.y);
for(int i=1; i<=10; i++) {
int r = tc & 3;
if(r==0) {
boolean exit = up(mapNew, i, redNew, blueNew);
if(exit) break;
}
else if(r==1) {
boolean exit = down(mapNew, i, redNew, blueNew);
if(exit) break;
}
else if(r==2) {
boolean exit = left(mapNew, i, redNew, blueNew);
if(exit) break;
}
else if(r==3) {
boolean exit = right(mapNew, i, redNew, blueNew);
if(exit) break;
}
tc >>= 2;
}
}
if(answer == Integer.MAX_VALUE)
System.out.println(-1);
else
System.out.println(answer);
}
}