[BOJ] 3055 탈출

Time Lapse :1hour 10min 52sec

3055.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
#include <iostream>
#include <memory.h>
#include <queue>
#include <algorithm>

using namespace std;
int R, C;
int dx[4] = {0,1,0,-1};
int dy[4] = {-1,0,1,0};
int gx, gy;
char MAP[50][50];

void printMAP(){
for(int i=0;i<C;i++){
for(int j=0;j<R;j++){
cout<<MAP[i][j]<<" ";
}
cout<<endl;
}
cout<<endl<<endl;
}

queue<pair<int,int>> BFS(queue<pair<int,int>> q){
queue<pair<int,int>> nq;
while(!q.empty()){
int x = q.front().second;
int y = q.front().first;
q.pop();
for(int i=0;i<4;i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(0<=nx&&nx<R && 0<=ny&&ny<C){
if(MAP[ny][nx]=='.'||MAP[ny][nx]=='S'){
MAP[ny][nx]='*';
nq.push(make_pair(ny,nx));
}
}
}
}
return nq;
}
queue<pair<int,int>> dBFS(queue<pair<int,int>> q){
queue<pair<int,int>> nq;
while(!q.empty()){
int x = q.front().second;
int y = q.front().first;
q.pop();
for(int i=0;i<4;i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(0<=nx&&nx<R && 0<=ny&&ny<C){
if(MAP[ny][nx]=='.'||MAP[ny][nx]=='D'){
MAP[ny][nx]='S';
nq.push(make_pair(ny,nx));
}
}
}
}
return nq;
}
int main(void){
cin>>C>>R;
int x, y;
queue<pair<int,int>> q;
queue<pair<int,int>> dq;
for(int i=0;i<C;i++)
for(int j=0;j<R;j++){
cin>>MAP[i][j];
if(MAP[i][j]=='S') {
dq.push(make_pair(i,j));
}
else if(MAP[i][j]=='D'){
gx = j; gy = i;
}
else if(MAP[i][j]=='*'){
q.push(make_pair(i,j));
}
}
int answer = 1;
while(!dq.empty()){
q=BFS(q);
dq=dBFS(dq);
if(MAP[gy][gx]=='S'){
cout<<answer<<endl;
return 0;
}
++answer;
}

cout<<"KAKTUS"<<endl;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/3055/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.