-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13460.java
More file actions
141 lines (129 loc) · 4.37 KB
/
Copy path13460.java
File metadata and controls
141 lines (129 loc) · 4.37 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
138
139
140
141
// https://www.acmicpc.net/problem/13460
// 구슬 탈출 2
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
static int n, m, map[][];
static final int EMPTY = 0, BLOCKED = 1, END = 2;
static int red, blue;
static final int[] DIR_X = {-1, 0, 1, 0}, DIR_Y = {0, 1, 0, -1};
static List<Integer> visited;
public static void main(String[] args) throws IOException {
BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(scan.readLine());
n = Integer.parseInt(st.nextToken())-2;
m = Integer.parseInt(st.nextToken())-2;
map = new int[n][m];
scan.readLine(); // drop
for(int i=0; i<n; i++){
String line = scan.readLine();
for(int j=0; j<m; j++){
switch(line.charAt(j+1)){
case '.': map[i][j] = EMPTY; break;
case '#': map[i][j] = BLOCKED; break;
case 'O': map[i][j] = END; break;
case 'R': red = getPos(i, j); break;
case 'B': blue = getPos(i, j); break;
}
}
}
Queue<Node> que = new LinkedList<>();
visited = new ArrayList<>();
visit(red, blue);
que.add(new Node(red, blue, 0));
while(!que.isEmpty()){
Node cur = que.poll();
//print(cur.red, cur.blue);
if(isEnd(cur.blue)) continue;
if(isEnd(cur.red)){
System.out.println(cur.depth);
return;
}
if(cur.depth == 10) continue;
for(int i=0; i<4; i++){
int[] result = move(cur.red, cur.blue, i);
if(!isVisited(result[0], result[1])){
visit(result[0], result[1]);
que.add(new Node(result[0], result[1], cur.depth+1));
}
}
}
System.out.println(-1);
}
public static int[] move(int red, int blue, int dir){
boolean redStop = false, blueStop = false;
int rx = getX(red), ry = getY(red), bx = getX(blue), by = getY(blue);
while(true){
if(redStop && blueStop)
return new int[]{getPos(rx, ry), getPos(bx, by)};
if(!redStop){
rx += DIR_X[dir]; ry += DIR_Y[dir];
if(!check(rx, ry)){
rx -= DIR_X[dir]; ry -= DIR_Y[dir];
redStop = true;
}
}
if(!blueStop){
bx += DIR_X[dir]; by += DIR_Y[dir];
if(!check(bx, by)){
bx -= DIR_X[dir]; by -= DIR_Y[dir];
blueStop = true;
}
}
if(rx == bx && ry == by){
if(map[rx][ry] == END){
redStop = true;
blueStop = true;
continue;
}
if(blueStop){
rx -= DIR_X[dir]; ry -= DIR_Y[dir];
redStop = true;
}
else{ //if(redStop)
bx -= DIR_X[dir]; by -= DIR_Y[dir];
blueStop = true;
}
}
if(map[rx][ry] == END) redStop = true;
if(map[bx][by] == END) blueStop = true;
}
}
public static boolean check(int x, int y){
if(x < 0 || n <= x || y < 0 || m <= y)
return false;
if(map[x][y] == BLOCKED)
return false;
return true;
}
public static void visit(int red, int blue){
visited.add(red * 64 + blue);
}
public static boolean isVisited(int red, int blue){
return visited.contains(red * 64 + blue);
}
public static boolean isEnd(int pos){
return map[getX(pos)][getY(pos)] == END;
}
public static int getPos(int x, int y){
return x * m + y;
}
public static int getX(int pos){
return pos / m;
}
public static int getY(int pos){
return pos % m;
}
public static void print(int red, int blue){
System.out.println("red=("+getX(red)+","+getY(red)+") / blue=("+getX(blue)+", "+getY(blue)+")");
}
}
class Node{
int red, blue, depth;
public Node(int red, int blue, int depth){
this.red = red;
this.blue = blue;
this.depth = depth;
}
}