-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13460.cpp
86 lines (73 loc) · 1.6 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
#include <iostream>
#include <queue>
using namespace std;
struct step{
int Rx, Ry;
int Bx, By;
int cnt;
};
char map[11][11];
bool check[11][11][11][11];
int N, M;
int dx[4] = { 1,-1,0,0 };
int dy[4] = { 0,0,1,-1 };
void move(int& rx, int& ry, int& distance, int& i){
while (map[rx + dx[i]][ry + dy[i]] != '#' && map[rx][ry] != 'O'){
rx += dx[i];
ry += dy[i];
distance += 1;
}
}
void bfs(int Rx, int Ry, int Bx, int By){
queue<step> q;
q.push({ Rx,Ry,Bx,By,0 });
check[Rx][Ry][Rx][Ry] = true;
while (!q.empty()){
int rx = q.front().Rx;
int ry = q.front().Ry;
int bx = q.front().Bx;
int by = q.front().By;
int cnt = q.front().cnt;
q.pop();
if (cnt >= 10) break;
for (int i = 0; i < 4; i++){
int nrx = rx, nry = ry, nbx = bx, nby = by;
int rc = 0, bc = 0, ncnt = cnt + 1;
move(nrx, nry, rc, i);
move(nbx, nby, bc, i);
if (map[nbx][nby] == 'O') continue;
if (map[nrx][nry] == 'O'){
cout << ncnt;
return;
}
// 공 겹치는 경우
if (nrx == nbx && nry == nby){
if (rc > bc) nrx -= dx[i], nry -= dy[i];
else nbx -= dx[i], nby -= dy[i];
}
if (check[nrx][nry][nbx][nby]) continue;
check[nrx][nry][nbx][nby] = true;
q.push({ nrx,nry,nbx,nby,ncnt });
}
}
cout << -1;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> N >> M;
int Rx = 0, Ry = 0, Bx = 0, By = 0;
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++){
cin >> map[i][j];
if (map[i][j] == 'R'){
Rx = i; Ry = j;
}
else if (map[i][j] == 'B'){
Bx = i; By = j;
}
}
}
bfs(Rx, Ry, Bx, By);
return 0;
}