-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13460.cpp
215 lines (213 loc) · 5.48 KB
/
13460.cpp
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
#include <bits/stdc++.h>
using namespace std;
#define MAX 10
#define endl "\n"
#define FastIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define INF 987654321
#define f first
#define s second
int n, m;
bool up(vector<vector<char>> &tmp, pair<int ,int> &p) {
tmp[p.f][p.s] = '.';
while(p.f >= 0) {
if(tmp[p.f][p.s] == '#' || tmp[p.f][p.s] == 'G') {
break;
}
if(tmp[p.f][p.s] == 'O') {
return true;
}
p.f--;
}
tmp[++p.f][p.s] = 'G';
return false;
}
bool down(vector<vector<char>> &tmp, pair<int, int> &p) {
tmp[p.f][p.s] = '.';
while(p.f < n) {
if(tmp[p.f][p.s] == '#' || tmp[p.f][p.s] == 'G') {
break;
}
if(tmp[p.f][p.s] == 'O') {
return true;
}
p.f++;
}
tmp[--p.f][p.s] = 'G';
return false;
}
bool left(vector<vector<char>> &tmp, pair<int, int> &p) {
tmp[p.f][p.s] = '.';
while(p.s >= 0) {
if(tmp[p.f][p.s] == '#' || tmp[p.f][p.s] == 'G') {
break;
}
if(tmp[p.f][p.s] == 'O') {
return true;
}
p.s--;
}
tmp[p.f][++p.s] = 'G';
return false;
}
bool right(vector<vector<char>> &tmp, pair<int ,int> &p) {
tmp[p.f][p.s] = '.';
while(p.s < m) {
if(tmp[p.f][p.s] == '#' || tmp[p.f][p.s] == 'G') {
break;
}
if(tmp[p.f][p.s] == 'O') {
return true;
}
p.s++;
}
tmp[p.f][--p.s] = 'G';
return false;
}
int terminal(int dir, vector<vector<char>> &tmp, pair<int, int> &b, pair<int, int> &r) {
switch(dir) {
case 0:
if(b.s == r.s) {
if(b.f < r.f) {
if(up(tmp, b)) {
return -1;
} else {
return up(tmp, r);
}
} else {
int ret = up(tmp, r);
if(up(tmp,b)) {
return -1;
}
return ret;
}
} else {
if(up(tmp, b)) {
return -1;
} else {
return up(tmp,r);
}
}
break;
case 1:
if(b.s == r.s) {
if(b.f > r.f) {
if(down(tmp, b)) {
return -1;
} else {
return down(tmp, r);
}
} else {
int ret = down(tmp, r);
if(down(tmp, b)) {
return -1;
}
return ret;
}
} else {
if(down(tmp, b)) {
return -1;
} else {
return down(tmp, r);
}
}
break;
case 2:
if(b.f == r.f) {
if(b.s < r.s) {
if(left(tmp, b)) {
return -1;
}
return left(tmp, r);
} else {
int ret = left(tmp, r);
if(left(tmp, b)) {
return -1;
}
return ret;
}
} else {
if(left(tmp, b)) {
return -1;
}
return left(tmp, r);
}
break;
case 3:
if(b.f == r.f) {
if(b.s > r.s) {
if(right(tmp, b)) {
return -1;
}
return right(tmp, r);
} else {
int ret = right(tmp, r);
if(right(tmp, b)) {
return -1;
}
return ret;
}
} else {
if(right(tmp, b)) {
return -1;
}
return right(tmp, r);
}
break;
}
return 1;
}
int ans;
void printBoard(vector<vector<char>> board);
void solve(int now, vector<vector<char>> board, pair<int ,int> blue, pair<int,int> red) {
if(now == 10) {
return;
}
// cout << now << endl;
// printBoard(board);
for(int i=0; i<4; i++) {
vector<vector<char>> tmp = board;
pair<int, int> b = blue;
pair<int, int> r = red;
int jud = terminal(i, tmp, b, r);
if(jud < 0) continue;
if(jud == 0) solve(now+1, tmp, b, r);
if(jud > 0) {
ans = min(ans, now);
return;
}
}
}
void printBoard(vector<vector<char>> board) {
cout << endl;
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
cout << board[i][j];
}
cout << endl;
}
}
int main(void) {
FastIO;
cin >> n >> m;
vector<vector<char>> board(n, vector<char>(m));
pair<int, int> red;
pair<int, int> blue;
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
cin >> board[i][j];
if(board[i][j] == 'R') {
red = {i, j};
board[i][j] = 'G';
}
if(board[i][j] == 'B') {
blue = {i, j};
board[i][j] = 'G';
}
}
}
ans = INF;
solve(0, board, blue, red);
if(ans == INF) ans = -2;
cout << ans+1 << endl;
return 0;
}