https://www.acmicpc.net/problem/3055
사악한 암흑의 군주 이민혁은 드디어 마법 구슬을 손에 넣었고, 그 능력을 실험해보기 위해 근처의 티떱숲에 홍수를 일으키려고 한다. 이 숲에는 고슴도치가 한 마리 살고 있다. 고슴도치는 제일 친한 친구인 비버의 굴로 가능한 빨리 도망가 홍수를 피하려고 한다.
티떱숲의 지도는 R행 C열로 이루어져 있다. 비어있는 곳은 '.'로 표시되어 있고, 물이 차있는 지역은 '*', 돌은 'X'로 표시되어 있다. 비버의 굴은 'D'로, 고슴도치의 위치는 'S'로 나타내어져 있다.
매 분마다 고슴도치는 현재 있는 칸과 인접한 네 칸 중 하나로 이동할 수 있다. (위, 아래, 오른쪽, 왼쪽) 물도 매 분마다 비어있는 칸으로 확장한다. 물이 있는 칸과 인접해있는 비어있는 칸(적어도 한 변을 공유)은 물이 차게 된다. 물과 고슴도치는 돌을 통과할 수 없다. 또, 고슴도치는 물로 차있는 구역으로 이동할 수 없고, 물도 비버의 소굴로 이동할 수 없다.
티떱숲의 지도가 주어졌을 때,
고슴도치가 안전하게 비버의 굴로 이동하기 위해 필요한 최소 시간을 구하는 프로그램을 작성하시오.
고슴도치는 물이 찰 예정인 칸으로 이동할 수 없다. 즉, 다음 시간에 물이 찰 예정인 칸으로 고슴도치는 이동할 수 없다. 이동할 수 있으면 고슴도치가 물에 빠지기 때문이다.
첫째 줄에 50보다 작거나 같은 자연수 R과 C가 주어진다.
다음 R개 줄에는 티떱숲의 지도가 주어지며, 문제에서 설명한 문자만 주어진다. 'D'와 'S'는 하나씩만 주어진다.
첫째 줄에 고슴도치가 비버의 굴로 이동할 수 있는 가장 빠른 시간을 출력한다. 만약, 안전하게 비버의 굴로 이동할 수 없다면, "KAKTUS"를 출력한다.
3 3
D.*
...
.S.
3
3 3
D.*
...
..S
KAKTUS
3 6
D...*.
.X.X..
....S.
6
5 4
.D.*
....
..X.
S.*.
....
4
3 3
S..
.D.
...
2
반례로 지도에 물 자체가 없는 경우 물의 최단 거리가 기록되지 않아 전부 0이었음
이렇게 되면 고슴도치의 최단 거리는 무조건 0보다 크거나 같으므로 고슴도치의 최단거리도 기록되지 않음
지도에 물이 없을 수도 있다는 것을 문제에서 알려줬어야 하는거 아닌가…
package graph_search;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
public class Main_3055 {
private static int height;
private static int width;
private static int[][] direction = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
private static int[][] waterMinDistance;
private static int[][] hedgehogMinDistance;
private static char[][] map;
private static boolean[][] visited;
private static Coordinate start;
private static Coordinate destination;
private static Queue<Coordinate> waterQueue = new LinkedList<>();
private static StringBuilder sb = new StringBuilder();
static class Coordinate {
int x;
int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) throws IOException {
input();
process();
output();
}
private static void input() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] temp = br.readLine().split(" ");
height = Integer.parseInt(temp[0]);
width = Integer.parseInt(temp[1]);
map = new char[width + 1][height + 1];
waterMinDistance = new int[width + 1][height + 1];
hedgehogMinDistance = new int[width + 1][height + 1];
visited = new boolean[width + 1][height + 1];
for (int y = 1; y <= height; y++) {
temp = br.readLine().split("");
for (int x = 1; x <= width; x++) {
map[x][y] = temp[x - 1].charAt(0);
if (map[x][y] == 'S') {
start = new Coordinate(x, y);
}
if (map[x][y] == 'D') {
destination = new Coordinate(x, y);
}
if (map[x][y] == '*') {
waterQueue.add(new Coordinate(x, y));
waterMinDistance[x][y] = 0;
visited[x][y] = true;
}
}
}
}
private static void process() {
bfsWater();
bfsHedgehog();
if (hedgehogMinDistance[destination.x][destination.y] == 0) {
sb.append("KAKTUS");
} else {
sb.append(hedgehogMinDistance[destination.x][destination.y]);
}
}
private static void bfsWater() {
while (!waterQueue.isEmpty()) {
Coordinate water = waterQueue.poll();
int x = water.x;
int y = water.y;
for (int dir = 0; dir < 4; dir++) {
int newX = x + direction[dir][0];
int newY = y + direction[dir][1];
if (newX <= 0 || newY <= 0 || newX > width || newY > height) {
continue;
}
if (visited[newX][newY]) {
continue;
}
if (map[newX][newY] != '.') {
continue;
}
waterMinDistance[newX][newY] = waterMinDistance[x][y] + 1;
visited[newX][newY] = true;
waterQueue.add(new Coordinate(newX, newY));
}
}
}
private static void bfsHedgehog() {
Queue<Coordinate> queue = new LinkedList<>();
visited = new boolean[width + 1][height + 1];
queue.add(start);
hedgehogMinDistance[start.x][start.y] = 0;
visited[start.x][start.y] = true;
while (!queue.isEmpty()) {
Coordinate hedgehog = queue.poll();
int x = hedgehog.x;
int y = hedgehog.y;
for (int dir = 0; dir < 4; dir++) {
int newX = x + direction[dir][0];
int newY = y + direction[dir][1];
if (newX <= 0 || newY <= 0 || newX > width || newY > height) {
continue;
}
if (visited[newX][newY]) {
continue;
}
if (map[newX][newY] == 'X' || map[newX][newY] == '*') {
continue;
}
if (waterMinDistance[newX][newY] != 0 && (hedgehogMinDistance[x][y] + 1 >= waterMinDistance[newX][newY])) {
continue;
}
hedgehogMinDistance[newX][newY] = hedgehogMinDistance[x][y] + 1;
visited[newX][newY] = true;
queue.add(new Coordinate(newX, newY));
}
}
}
private static void output() {
System.out.println(sb);
}
}