'S'
로 표시-1
로 표기계속 -1 이 출력됨
count 하는 방법이 비효율적인 것 같음
현재 방법으로는 고슴도치를 이동할 때, 물, X, 지나온 경로는 피할 수 있지만 최소 경로를 찾는 방법은 아닌 것 같음
탈출을 할 수 없는 경우에 대한 판단과 최종 count를 함수 return 으로 해야할지 변수로 해야할지 모르겠음
⭐️ BFS 로 풀이 + DP 발상
queue
사용queue
사용#include <iostream>
#include <queue>
#include <string>
#include <cstring>
using namespace std;
struct pos {
int x;
int y;
};
int r,c,sx,sy;
char board[51][51];
queue<pos> water;
int wdist[51][51];
int sdist[51][51];
// 상하좌우
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
void waterCheck() {
while(!water.empty()) {
pos p=water.front();
int x=p.x;
int y=p.y;
for(int i=0;i<4;i++) {
int nx=x+dx[i];
int ny=y+dy[i];
int cnt=wdist[x][y];
if(nx<0 || nx>=r || ny<0 || ny>=c) continue;
if(board[nx][ny]!='X' && board[nx][ny]!='D'&& wdist[nx][ny]==-1) {
water.push({nx,ny});
wdist[nx][ny]=cnt+1;
}
}
water.pop();
}
}
void sCheck() {
queue<pos> q;
q.push({sx,sy});
sdist[sx][sy]=0;
while(!q.empty()) {
pos sp=q.front();
int x=sp.x;
int y=sp.y;
int cnt=sdist[x][y];
for(int i=0;i<4;i++) {
int nx=x+dx[i];
int ny=y+dy[i];
if(nx<0 || nx>=r || ny<0 || ny>=c) continue;
if (board[nx][ny]=='X' || board[nx][ny]=='D' || sdist[nx][ny]!=-1) continue;
q.push({nx,ny});
sdist[nx][ny]=cnt+1;
}
q.pop();
}
}
int main() {
int fx,fy;
cin >> r >> c;
memset(wdist,-1,sizeof(wdist));
memset(sdist,-1,sizeof(sdist));
for(int i=0;i<r;i++) {
for(int j=0;j<c;j++) {
cin >> board[i][j];
if(board[i][j] == 'S') sx = i, sy = j;
else if(board[i][j] == 'D') fx = i, fy = j;
else if(board[i][j] == '*') {
water.push({i,j});
wdist[i][j]=0;
}
}
}
waterCheck();
sCheck();
int ans=30000;
for (int i=0; i<4; i++) {
int nx=fx+dx[i];
int ny=fy+dy[i];
if(nx<0 || nx>=r || ny<0 || ny>=c) continue;
// 고슴도치가 더 늦게 도착할 경우이면서 물이 갈 수 있는 위치인 경우
if(wdist[nx][ny] > 0 && sdist[nx][ny] >= wdist[nx][ny]) continue;
// 고슴도치가 갈 수 없는 위치일 경우
if(sdist[nx][ny] == -1) continue;
ans=min(ans, sdist[nx][ny]+1);
}
if (ans==30000) cout << "KAKTUS";
else cout << ans;
}