https://www.acmicpc.net/problem/3055
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main_3055_이광용 {
private static Queue<int[]> waterQ;
private static Queue<int[]> playerQ;
private static int R;
private static int C, sX, sY, eX, eY, ans;
private static int[] dx = {0, 1, 0, -1};
private static int[] dy = {1, 0, -1, 0};
private static char[][] map;
private static int[][] waterDepthMap;//물이 퍼지는 시간을 기록할 map
private static boolean[][] isVisited;//BFS에서 방문체크할 배열
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
map = new char[R][C];
waterDepthMap = new int[R][C];
for(int i = 0; i < R; i++) {
//고슴도치 BFS탐색을 할 때, 물의 시간보다 작아야 방문할 수 있다.
//따라서 물이 가지 않은 위치를 고슴도치가 방문할 수 있도록 MAX로 초기화 시켜둔다.
for(int j = 0 ; j <C ; j++) {
waterDepthMap[i][j] = Integer.MAX_VALUE;
}
}
waterQ = new LinkedList<>();
playerQ = new LinkedList<>();
isVisited = new boolean[R][C];
for(int i = 0; i < R; i++) {
String s = br.readLine();
for(int j = 0; j <C; j++) {
map[i][j] = s.charAt(j);
switch (map[i][j]) {
case 'D': //목표위치
eX = i;
eY = j;
break;
case '*': //물
isVisited[i][j] = true; //bfs에서 방문표시는 방문 예정일 때 하므로 입력받을 때, 시작위치 방문 체크한다.
waterQ.add(new int[] {i, j, 0}); //마지막 인자는 depth(분)
break;
case 'S': //고슴도치
sX = i;
sY = j;
playerQ.add(new int[] {i, j, 0});
map[i][j] = '.';//먼저 물의 BFS를 하게 되는데 고슴도치 시작위치 또한 갈 수 있는 곳이므로
//빈 공간으로 표시해둔다.
break;
case 'X': //돌
waterDepthMap[i][j] = 0; //돌은 고슴도치가 방문할 수 없으므로 0으로 바꿔놓는다.
break;
default:
break;
}
}
}
waterBFS();
playerBFS();
if(ans == 0) System.out.println("KAKTUS");
else System.out.println(ans);
}
private static void playerBFS() {
isVisited = new boolean[R][C];//방문 배열 초기화
isVisited[sX][sY] = true; //시작 위치 방문 체크
while (!playerQ.isEmpty()) {
int[] tmp = playerQ.peek();
int tmpX = tmp[0];
int tmpY = tmp[1];
int tmpD = tmp[2];
if(tmpX == eX && tmpY ==eY) {//목표 위치 도착
ans = tmpD;
break;
}
playerQ.poll();
for(int i = 0; i <4; i++) {
int nx = tmpX + dx[i];
int ny = tmpY + dy[i];
if(inRange(nx, ny) && !isVisited[nx][ny]
&& tmpD + 1 < waterDepthMap[nx][ny] && map[nx][ny] != 'X') {
//waterDepthMap으로 물 위치를 피하고 map으로 돌의 위치를 피한다.
isVisited[nx][ny] = true; //방문 예정일 때 방문체크
playerQ.add(new int[] {nx, ny, tmpD + 1}); //시간 +1해서 큐에 삽입
}
}
}
}
private static void waterBFS() {
while (!waterQ.isEmpty()) {
int[] tmp = waterQ.peek();
int tmpX = tmp[0];
int tmpY = tmp[1];
int tmpD = tmp[2];
waterQ.poll();
waterDepthMap[tmpX][tmpY] = tmpD; //몇 분에 방문했는지 기록
for(int i = 0; i <4; i++) {
int nx = tmpX + dx[i];
int ny = tmpY + dy[i];
if(inRange(nx, ny) && !isVisited[nx][ny] && map[nx][ny] == '.') {
isVisited[nx][ny] = true; //방문 예정일 때 방문체크
waterQ.add(new int[] {nx, ny, tmpD + 1}); //시간 +1해서 큐에 삽입
}
}
}
}
private static boolean inRange(int nx, int ny) {
if(nx < 0 || nx >= R || ny < 0 || ny >= C) return false;
else return true;
}
}