- toc {:toc}
๋ฌธ์
์ฌ์ ํ ์ํ์ ๊ตฐ์ฃผ ์ด๋ฏผํ์ ๋๋์ด ๋ง๋ฒ ๊ตฌ์ฌ์ ์์ ๋ฃ์๊ณ , ๊ทธ ๋ฅ๋ ฅ์ ์คํํด๋ณด๊ธฐ ์ํด ๊ทผ์ฒ์ ํฐ๋ฑ์ฒ์ ํ์๋ฅผ ์ผ์ผํค๋ ค๊ณ ํ๋ค. ์ด ์ฒ์๋ ๊ณ ์ด๋์น๊ฐ ํ ๋ง๋ฆฌ ์ด๊ณ ์๋ค. ๊ณ ์ด๋์น๋ ์ ์ผ ์นํ ์น๊ตฌ์ธ ๋น๋ฒ์ ๊ตด๋ก ๊ฐ๋ฅํ ๋นจ๋ฆฌ ๋๋ง๊ฐ ํ์๋ฅผ ํผํ๋ ค๊ณ ํ๋ค.
ํฐ๋ฑ์ฒ์ ์ง๋๋ Rํ C์ด๋ก ์ด๋ฃจ์ด์ ธ ์๋ค. ๋น์ด์๋ ๊ณณ์ โ.โ๋ก ํ์๋์ด ์๊ณ , ๋ฌผ์ด ์ฐจ์๋ ์ง์ญ์ โ*โ, ๋์ โXโ๋ก ํ์๋์ด ์๋ค. ๋น๋ฒ์ ๊ตด์ โDโ๋ก, ๊ณ ์ด๋์น์ ์์น๋ โSโ๋ก ๋ํ๋ด์ด์ ธ ์๋ค.
๋งค ๋ถ๋ง๋ค ๊ณ ์ด๋์น๋ ํ์ฌ ์๋ ์นธ๊ณผ ์ธ์ ํ ๋ค ์นธ ์ค ํ๋๋ก ์ด๋ํ ์ ์๋ค. (์, ์๋, ์ค๋ฅธ์ชฝ, ์ผ์ชฝ) ๋ฌผ๋ ๋งค ๋ถ๋ง๋ค ๋น์ด์๋ ์นธ์ผ๋ก ํ์ฅํ๋ค. ๋ฌผ์ด ์๋ ์นธ๊ณผ ์ธ์ ํด์๋ ๋น์ด์๋ ์นธ(์ ์ด๋ ํ ๋ณ์ ๊ณต์ )์ ๋ฌผ์ด ์ฐจ๊ฒ ๋๋ค. ๋ฌผ๊ณผ ๊ณ ์ด๋์น๋ ๋์ ํต๊ณผํ ์ ์๋ค. ๋, ๊ณ ์ด๋์น๋ ๋ฌผ๋ก ์ฐจ์๋ ๊ตฌ์ญ์ผ๋ก ์ด๋ํ ์ ์๊ณ , ๋ฌผ๋ ๋น๋ฒ์ ์๊ตด๋ก ์ด๋ํ ์ ์๋ค.
ํฐ๋ฑ์ฒ์ ์ง๋๊ฐ ์ฃผ์ด์ก์ ๋, ๊ณ ์ด๋์น๊ฐ ์์ ํ๊ฒ ๋น๋ฒ์ ๊ตด๋ก ์ด๋ํ๊ธฐ ์ํด ํ์ํ ์ต์ ์๊ฐ์ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
๊ณ ์ด๋์น๋ ๋ฌผ์ด ์ฐฐ ์์ ์ธ ์นธ์ผ๋ก ์ด๋ํ ์ ์๋ค. ์ฆ, ๋ค์ ์๊ฐ์ ๋ฌผ์ด ์ฐฐ ์์ ์ธ ์นธ์ผ๋ก ๊ณ ์ด๋์น๋ ์ด๋ํ ์ ์๋ค. ์ด๋ํ ์ ์์ผ๋ฉด ๊ณ ์ด๋์น๊ฐ ๋ฌผ์ ๋น ์ง๊ธฐ ๋๋ฌธ์ด๋ค.ย
์ ๋ ฅ
์ฒซ์งธ ์ค์ 50๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ์์ฐ์ R๊ณผ C๊ฐ ์ฃผ์ด์ง๋ค.
๋ค์ R๊ฐ ์ค์๋ ํฐ๋ฑ์ฒ์ ์ง๋๊ฐ ์ฃผ์ด์ง๋ฉฐ, ๋ฌธ์ ์์ ์ค๋ช ํ ๋ฌธ์๋ง ์ฃผ์ด์ง๋ค. โDโ์ โSโ๋ ํ๋์ฉ๋ง ์ฃผ์ด์ง๋ค.
์ถ๋ ฅ
์ฒซ์งธ ์ค์ ๊ณ ์ด๋์น๊ฐ ๋น๋ฒ์ ๊ตด๋ก ์ด๋ํ ์ ์๋ ๊ฐ์ฅ ๋น ๋ฅธ ์๊ฐ์ ์ถ๋ ฅํ๋ค. ๋ง์ฝ, ์์ ํ๊ฒ ๋น๋ฒ์ ๊ตด๋ก ์ด๋ํ ์ ์๋ค๋ฉด, โKAKTUSโ๋ฅผ ์ถ๋ ฅํ๋ค.
ํ์ด
๊ฐ๋ฅํ ๊ฑฐ๋ฆฌ ์ค โ์ต๋จ ๊ฑฐ๋ฆฌโ๋ฅผ ์ฐพ๋ ๊ฒ์ด ๋ชฉ์ ์ด๊ธฐ ๋๋ฌธ์ ๊ฐ์ฅ ๋จผ์ BFS(Breadth-First Search)๋ฅผ ์๊ฐํ๋ค. ๊ธฐ๋ณธ์ ์ธ BFS์์ ๋ค๋ฅธ ์ ์ ๋ฌผ์ ์ด๋์ ์ถ๊ฐํ๋ค๋ ์ ์ด๋ค. ์ง๋๋ฅผ ๋ํ๋ด๋ board์ ์ด๋ ๊ฑฐ๋ฆฌ๋ฅผ ๋ํ๋ด๋ dist ๋ฐฐ์ด์ ์ฌ์ฉํ์ฌ ์กฐ๊ฑด์ ์ค์ ํ๋ค. ๋ฌผ์ ์ด๋์ ๊ฒฝ์ฐ S์ ์ด๋ ๊ฑฐ๋ฆฌ๊ฐ ๊ฐ์ ์ขํ๊ฐ ํ์ ๋ค์ด๊ฐ์ ๊ฒฝ์ฐ๋ ๊ณ ๋ คํด์ค์ผ ํ๊ธฐ ๋๋ฌธ์ check ๋ณ์๋ฅผ ํตํด ์ด๋๊ฑฐ๋ฆฌ๊ฐ 1๊ณผ ๊ฐ์ด ๊ฐ์ ๊ฒฝ์ฐ์๋ water() ํจ์๋ฅผ ์คํํ์ง ์๊ณ , check 0, dist 1 ๊ณผ ๊ฐ์ด ์ด๋ ๊ฑฐ๋ฆฌ์ ์ฐจ์ด๊ฐ ์๋ ๊ฒฝ์ฐ, ํด์ด ๋ฌ๋ผ์ง๋ ๊ฒฝ์ฐ์๋ง water() ํจ์๋ฅผ ์คํํด ์ด๋ ํด์ ๋ง๊ฒ ๋ฌผ์ด ์ด๋ํ ์ ์๊ฒ ํ๋ค.
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
char board[50][50];
int dist[50][50];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
queue<pair<int, int>> q;
queue<pair<int, int>> w;
int R, C;
void water(){
int num = w.size();
while(num--){
pair<int, int> cur = w.front();
w.pop();
for(int dir=0; dir<4; dir++){
int nx = cur.X + dx[dir];
int ny = cur.Y + dy[dir];
if ((nx < 0)||(nx >= R)||(ny < 0)||(ny >= C)) continue;
if (board[nx][ny] != '.') continue;
board[nx][ny] = '*';
w.push({nx, ny});
}
}
}
int escape(){
int check = -1;
while(!q.empty()){
pair<int, int> cur = q.front();
q.pop();
if (check != dist[cur.X][cur.Y]){
water();
check = dist[cur.X][cur.Y];
}
for (int dir=0; dir<4; dir++) {
int nx = cur.X + dx[dir];
int ny = cur.Y + dy[dir];
if ((nx < 0)||(nx >= R)||(ny < 0)||(ny >= C)) continue;
if (board[nx][ny] == '*' || board[nx][ny] == 'X' || dist[nx][ny] != -1) continue;
dist[nx][ny] = dist[cur.X][cur.Y] + 1;
if (board[nx][ny] == 'D') return dist[nx][ny];
q.push({nx, ny});
}
}
return -1;
};
int main(){
ios::sync_with_stdio(true);
cin.tie(), cout.tie();
cin >> R >> C;
for(int row=0; row<R; row++){
for(int col=0; col<C; col++){
cin >> board[row][col];
dist[row][col] = -1;
if (board[row][col] == 'S'){
q.push({row, col});
dist[row][col] = 0;
}
else if (board[row][col] == '*'){
w.push({row, col});
}
}
}
int dest_dist = escape();
if (dest_dist == -1){
cout << "KAKTUS" << endl;
}
else{
cout << dest_dist << endl;
}
return 0;
}