-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path[BOJ] 13460 구술탈출2.cpp
More file actions
142 lines (121 loc) · 3.19 KB
/
[BOJ] 13460 구술탈출2.cpp
File metadata and controls
142 lines (121 loc) · 3.19 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
142
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
struct Node {
int depth;
int rx, ry, bx, by;
Node(int e, int a,int b, int c, int d): depth(e), rx(a), ry(b), bx(c), by(d){};
};
int r_x, r_y, b_x, b_y, h_x, h_y;
int n, m;
int ans;
int map[10][10];
bool visit[10][10][10][10];
// 동 북 서 남
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
void move(int &x, int &y, int d) {
while (1) {
x += dx[d];
y += dy[d];
if (map[x][y] == 1) {
x -= dx[d];
y -= dy[d];
break;
}
else if (map[x][y] == 2) {
break;
}
}
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
ans = -1;
cin >> n >> m;
for(int i=0; i<n; i++){
string s;
cin >> s;
for(int j=0; j<m; j++){
switch (s[j]) {
case '#' :
map[i][j] = 1;
break;
case '.' :
map[i][j] = 0;
break;
case 'O' :
map[i][j] = 2;
h_x = i;
h_y = j;
break;
case 'R' :
r_x = i;
r_y = j;
break;
case 'B' :
b_x = i;
b_y = j;
break;
}
}
}
queue<Node> q;
q.push( Node(0, r_x, r_y, b_x, b_y) );
visit[r_x][r_y][b_x][b_y] = true;
while(!q.empty()) {
Node node = q.front();
q.pop();
if (node.depth > 10)
break;
if (node.rx == h_x && node.ry == h_y) {
ans = node.depth;
break;
}
for (int i = 0; i < 4; ++i) {
int rx = node.rx, ry = node.ry;
int bx = node.bx, by = node.by;
move(rx, ry, i);
move(bx, by, i);
/*
Blue 공이 Hall에 빠지면 안된다.
*/
if (bx == h_x && by == h_y)
continue;
/*
row or column
같은 선상에 있었다면 예외처리를 해줘야한다.
0 : 서
1 : 북
2 : 동
3 : 남
*/
if (rx == bx && ry == by) {
switch (i) {
case 0: // 동
node.rx < node.bx ? rx-- : bx--; break;
case 2: // 서
node.rx < node.bx ? bx++ : rx++; break;
case 1: // 북
node.ry < node.by ? ry-- : by--; break;
case 3: // 남
node.ry < node.by ? by++ : ry++; break;
}
}
/*
visit처리 안해줘도 AC
하지만 안해주면
엄청 난 시간이 더 소요된다.
*/
if (!visit[rx][ry][bx][by]) {
q.push( Node(node.depth + 1,rx,ry,bx,by ) );
visit[rx][ry][bx][by] = true;
}
}
}
cout << ans;
return 0;
}