구슬 탈출 시리즈
2048문제와 같이 백준 빡구현 문제 중 하나로 계속 들어왔던 문제라 한번 쭉 풀어봤다.
골드 1치고는 아이디어도 간단하고 구현을 하는 것에도 그렇게 어렵지 않았던 것 같다.
1번의 변형으로 2, 3, 4를 쉽게 풀 수 있으므로 1번만 신경써주면 됐다.
우선 구현하기 편했던 것으로 테두리가 벽으로 둘려 쌓여있는 조건이 있었다.
이미 왔던 경로를 다시하지 않도록 체크는 map(파랑 x, 파랑 y, 빨강 x, 빨강 y)로 구현했다.
4가지가 모두 같아야지 이미 지나온 경로라고 할 수 있기 때문이다.
다음 상하좌우로 움직이는 것은 하나만 구현하면 나머지는 복붙으로 조금씩 바꾸면 가능하다.
주의해야 하는 것은 빨강과 파랑이 같은 줄에 있을 때 판을 기울이면 순서가 지켜져야 한다는 것.
그래서 이동하는 방향으로 벽에 부딪힐 때까지 공들을 전부 옮기고 그 때 파란 공과 빨간 공이 겹치면
이동하는 방향 벽에 더 멀었던 공을 반대쪽으로 1만큼 옮겨주는 방법을 사용했다.
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
int main()
{
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long mod = 1'000'000'007;
using P=pair<ll,ll>;
using PP = pair<P,P>;
long long a, b, c, d;
long long ans = 1e9;
cin >> n >> m;
vector<string> board(n);
for(int i=0;i<n;i++) cin>>board[i];
P red; //뺄거
P blue; // 들어가면 안됨
P holl;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(board[i][j]=='B')blue={i,j};
else if(board[i][j]=='R')red={i,j};
if(board[i][j]=='O')holl={i,j};
}
}
//dx dy 오른쪽, 왼쪽, 위쪽, 아래쪽임
map<PP, int> mp;
int hx=holl.first;
int hy=holl.second;
bool pause=0;
int an=1e6;
function<void(int, int, int, int ,int ,int)>
dfs=[&](int cnt, int dir, int bx, int by, int rx, int ry){
pause=0;
flag=0;
if(mp[{{bx, by}, {rx, ry}}]==0 || mp[{{bx, by}, {rx, ry}}]>cnt){
mp[{{bx, by}, {rx, ry}}]=cnt;
}
else return;
if(cnt==11){
return;
}
for(int x=0;x<4;x++){
pause=0;
flag=0;
int pbx=bx;
int pby=by;
int prx=rx;
int pry=ry;
if(x==dir)continue;
if(x==0){ //오른쪽
if(pby>pry){
while(board[pbx][pby]!='#'){
if(pbx==hx && pby==hy){
pause=1;//구멍에 들어감
break;;
}
pby++;
}
pby-=1;
if(pause)continue;
else{
while(board[prx][pry]!='#'){
if(prx==hx && pry==hy){
flag=1;//구멍에 들어감
break;
}
pry++;
}
pry-=1;
if(pbx==prx && pby==pry)pry-=1;
}
if(!pause && flag){
an=min(an, cnt);
continue;
}
}
else{
while(board[pbx][pby]!='#'){
if(pbx==hx && pby==hy){
pause=1;//구멍에 들어감
break;
}
pby++;
}
pby-=1;
if(pause)continue;
else{
while(board[prx][pry]!='#'){
if(prx==hx && pry==hy){
flag=1;//구멍에 들어감
break;
}
pry++;
}
pry-=1;
if(pbx==prx && pby==pry)pby-=1;
}
if(!pause && flag){
an=min(an, cnt);
continue;
}
}
}
else if(x==1){ //왼쪽
if(pby<pry){
while(board[pbx][pby]!='#'){
if(pbx==hx && pby==hy){
pause=1;//구멍에 들어감
break;
}
pby--;
}
pby+=1;
if(pause)continue;
else{
while(board[prx][pry]!='#'){
if(prx==hx && pry==hy){
flag=1;//구멍에 들어감
break;
}
pry--;
}
pry+=1;
if(pbx==prx && pby==pry)pry+=1;
}
if(!pause && flag){
an=min(an, cnt);
continue;
}
}
else{
while(board[pbx][pby]!='#'){
if(pbx==hx && pby==hy){
pause=1;//구멍에 들어감
break;
}
pby--;
}
pby+=1;
if(pause)continue;
else{
while(board[prx][pry]!='#'){
if(prx==hx && pry==hy){
flag=1;//구멍에 들어감
break;
}
pry--;
}
pry+=1;
if(pbx==prx && pby==pry)pby+=1;
}
if(!pause && flag){
an=min(an, cnt);
continue;
}
}
}
else if(x==2){ //위쪽
if(pbx<prx){
while(board[pbx][pby]!='#'){
if(pbx==hx && pby==hy){
pause=1;//구멍에 들어감
break;
}
pbx--;
}
pbx+=1;
if(pause)continue;
else{
while(board[prx][pry]!='#'){
if(prx==hx && pry==hy){
flag=1;//구멍에 들어감
break;
}
prx--;
}
prx+=1;
if(pbx==prx && pby==pry)prx+=1;
}
if(!pause && flag){
an=min(an, cnt);
continue;
}
}
else{
while(board[pbx][pby]!='#'){
if(pbx==hx && pby==hy){
pause=1;//구멍에 들어감
break;
}
pbx--;
}
pbx+=1;
if(pause)continue;
else{
while(board[prx][pry]!='#'){
if(prx==hx && pry==hy){
flag=1;//구멍에 들어감
break;
}
prx--;
}
prx+=1;
if(pbx==prx && pby==pry)pbx+=1;
}
if(!pause && flag){
an=min(an, cnt);
continue;
}
}
}
else if(x==3){ //아래쪽
if(pbx>prx){
while(board[pbx][pby]!='#'){
if(pbx==hx && pby==hy){
pause=1;//구멍에 들어감
break;
}
pbx++;
}
pbx-=1;
if(pause)continue;
else{
while(board[prx][pry]!='#'){
if(prx==hx && pry==hy){
flag=1;//구멍에 들어감
break;
}
prx++;
}
prx-=1;
if(pbx==prx && pby==pry)prx-=1;
}
if(!pause && flag){
an=min(an, cnt);
continue;
}
}
else{
while(board[pbx][pby]!='#'){
if(pbx==hx && pby==hy){
pause=1;//구멍에 들어감
break;
}
pbx++;
}
pbx-=1;
if(pause)continue;
else{
while(board[prx][pry]!='#'){
if(prx==hx && pry==hy){
flag=1;//구멍에 들어감
break;
}
prx++;
}
prx-=1;
if(pbx==prx && pby==pry)pbx-=1;
}
if(!pause && flag){
an=min(an, cnt);
continue;
}
}
}
dfs(cnt+1, x, pbx, pby, prx, pry);
}
};
dfs(1, -1, blue.first, blue.second, red.first, red.second);
//cout << an << '\n';
if(an<=10)cout<<1;
else cout<<0;
return 0;
}
전체 코드가 길지만 같은 내용의 중복이다.
2번은 횟수를 출력하면 되므로 an을 출력하면 되고
4번은 max 10의 조건이 사라진 것이니 DFS에서 그 조건을 없애주면 된다.
3번은 자주 쓰는 방법으로 경로 인자를 DFS 함수에 추가해서 해결했다.