This repository was archived by the owner on Dec 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBOJ13460.java
More file actions
137 lines (120 loc) · 4.56 KB
/
Copy pathBOJ13460.java
File metadata and controls
137 lines (120 loc) · 4.56 KB
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
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class BOJ13460 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
char[][] map = new char[n][m];
boolean[][][][] visited = new boolean[n][m][n][m];
int[] xDir = new int[]{-1, 0, 1, 0};
int[] yDir = new int[]{0, 1, 0, -1};
Queue<Point> pointQueue = new LinkedList<>();
int redX = 0;
int redY = 0;
int blueX = 0;
int blueY = 0;
for (int i = 0; i < n; i++) {
String mapStr = sc.next();
for (int j = 0; j < m; j++) {
map[i][j] = mapStr.charAt(j);
if (map[i][j] == 'R') {
map[i][j] = '.';
redX = i;
redY = j;
} else if (map[i][j] == 'B') {
map[i][j] = '.';
blueX = i;
blueY = j;
}
}
}
pointQueue.offer(new Point(redX, redY, blueX, blueY, 0));
visited[redX][redY][blueX][blueY] = true;
while (!pointQueue.isEmpty()) {
Point point = pointQueue.poll();
if (point.move > 9) break;
for (int i = 0; i < 4; i++) {
redX = point.redX;
redY = point.redY;
blueX = point.blueX;
blueY = point.blueY;
int move = point.move;
boolean inHole = false;
while (map[redX + xDir[i]][redY + yDir[i]] != '#') {
if (map[redX + xDir[i]][redY + yDir[i]] == 'O') {
inHole = true;
break;
}
redX += xDir[i];
redY += yDir[i];
}
boolean bluehole = false;
while (map[blueX + xDir[i]][blueY + yDir[i]] != '#') {
if (map[blueX + xDir[i]][blueY + yDir[i]] == 'O') {
bluehole = true;
}
blueX += xDir[i];
blueY += yDir[i];
}
if (!bluehole) {
if (inHole) {
System.out.println(move + 1);
return;
}
if (redX == blueX && redY == blueY) {
switch (i) {
case 0:
if (point.redX > point.blueX) {
redX = blueX + 1;
} else {
blueX = redX + 1;
}
break;
case 1:
if (point.redY > point.blueY) {
blueY = redY - 1;
} else {
redY = blueY - 1;
}
break;
case 2:
if (point.redX > point.blueX) {
blueX = redX - 1;
} else {
redX = blueX - 1;
}
break;
case 3:
if (point.redY > point.blueY) {
redY = blueY + 1;
} else {
blueY = redY + 1;
}
break;
}
}
if (visited[redX][redY][blueX][blueY]) continue;
visited[redX][redY][blueX][blueY] = true;
pointQueue.offer(new Point(redX, redY, blueX, blueY, move + 1));
}
}
}
System.out.println(-1);
}
static class Point {
int redX;
int redY;
int blueX;
int blueY;
int move;
public Point(int redX, int redY, int blueX, int blueY, int move) {
this.redX = redX;
this.redY = redY;
this.blueX = blueX;
this.blueY = blueY;
this.move = move;
}
}
}