https://school.programmers.co.kr/learn/courses/30/lessons/159993
1 x 1 크기의 칸들로 이루어진 직사각형 격자 형태의 미로에서 탈출하려고 합니다. 각 칸은 통로 또는 벽으로 구성되어 있으며, 벽으로 된 칸은 지나갈 수 없고 통로로 된 칸으로만 이동할 수 있습니다. 통로들 중 한 칸에는 미로를 빠져나가는 문이 있는데, 이 문은 레버를 당겨서만 열 수 있습니다. 레버 또한 통로들 중 한 칸에 있습니다. 따라서, 출발 지점에서 먼저 레버가 있는 칸으로 이동하여 레버를 당긴 후 미로를 빠져나가는 문이 있는 칸으로 이동하면 됩니다. 이때 아직 레버를 당기지 않았더라도 출구가 있는 칸을 지나갈 수 있습니다. 미로에서 한 칸을 이동하는데 1초가 걸린다고 할 때, 최대한 빠르게 미로를 빠져나가는데 걸리는 시간을 구하려 합니다.
미로를 나타낸 문자열 배열 maps가 매개변수로 주어질 때, 미로를 탈출하는데 필요한 최소 시간을 return 하는 solution 함수를 완성해주세요. 만약, 탈출할 수 없다면 -1을 return 해주세요.
5 ≤ maps의 길이 ≤ 100
5 ≤ maps[i]의 길이 ≤ 100
maps[i]는 다음 5개의 문자들로만 이루어져 있습니다.
시작 지점과 출구, 레버는 항상 다른 곳에 존재하며 한 개씩만 존재합니다.
출구는 레버가 당겨지지 않아도 지나갈 수 있으며, 모든 통로, 출구, 레버, 시작점은 여러 번 지나갈 수 있습니다.
maps | result |
---|---|
["SOOOL","XXXXO","OOOOO","OXXXX","OOOOE"] | 16 |
["LOOXS","OOOOX","OOOOO","OOOOO","EOOOO"] | -1 |
반드시 레버를 지나면서 끝 지점까지의 최단거리를 구하는 문제이므로, 시작지점 S
에서 레버 L
까지, 레버 L
에서 끝 지점 E
까지의 최단거리를 각각 구해서 더해주는 문제라고 생각할 수 있다.
최단거리 문제에는 많은 해법이 존재하겠지만, 나는 각 칸에 떨어진 거리를 최소값으로 유지하는 방식으로 풀었다. (dynamic programming) 우선 모든 경우의 수를 탐색해봐야 하므로 처음에는 dfs (stack)를 이용하여 풀었다. Java의 경우 stack과 while문으로 구현을 하였는데 별 문제없이 해결이 되었다.
하지만 Python에서 처음에 dfs를 재귀함수로 구현하여 제출했을 때, 몇몇 케이스에서 런타임 에러가 발생하였다. 그래서 입력을 100*100 사이즈의 맵으로 줘보니, RecursionError라는 녀석이였다. 경우가 많아질 수록 스택이 엄청나게 쌓이는 모양이였다. 그래서 Java와 같이 stack과 while문의 조합으로 구현했더니, 이번엔 시간초과가 떴다. Java와 완전히 동일한 로직이였는데 말이다.. 그래서 찾아보니, dfs는 최단경로를 보장하지 못하는 모양이였다. 그래서 나머진 그대로 두고 stack을 queue로 바꿔주기만 했더니, 속도가 눈에 띄게 빨라지면서 해결되었다.
import java.util.*;
public class MazeEscape {
char[][] map;
public int solution(String[] maps) {
map = new char[maps.length][];
for (int i = 0; i < map.length; i++) {
map[i] = maps[i].toCharArray();
}
int[] start = findXY('S');
int[] lever = findXY('L');
int[] exit = findXY('E');
int sTol = shortDistance(start, lever);
int lToe = shortDistance(lever, exit);
if (sTol == -1 || lToe == -1) return -1;
return sTol + lToe;
}
private int[] findXY(char c) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (map[i][j] == c) return new int[] {i, j};
}
}
return null;
}
private int shortDistance(int[] init, int[] fin) {
boolean[][] visited = new boolean[map.length][map[0].length];
int[][] dp = new int[map.length][map[0].length];
int[][] dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
Stack<int[]> stack = new Stack<>(){{push(init);}};
while (!stack.isEmpty()) {
int[] curr = stack.pop();
int x = curr[0], y = curr[1];
visited[x][y] = true;
for (int[] d : dir) {
int nx = x + d[0], ny = y + d[1];
if (!checkBoundary(nx, ny)) continue;
if (map[nx][ny] == 'X') continue;
if (!visited[nx][ny]) {
stack.push(new int[] {nx, ny});
dp[nx][ny] = dp[x][y] + 1;
visited[nx][ny] = true;
} else {
if (dp[nx][ny] > dp[x][y] + 1) {
stack.push(new int[] {nx, ny});
dp[nx][ny] = dp[x][y] + 1;
}
}
}
}
return dp[fin[0]][fin[1]] != 0 ? dp[fin[0]][fin[1]] : -1;
}
private boolean checkBoundary(int x, int y) {
return x >= 0 && y >= 0 && x < map.length && y < map[0].length;
}
}
def solution(maps):
sx, sy = findXY("S", maps)
ls, ly = findXY("L", maps)
ex, ey = findXY("E", maps)
start_to_lever = short_distance((sx, sy), (ls, ly), maps)
lever_to_end = short_distance((ls, ly), (ex, ey), maps)
if start_to_lever != -1 and lever_to_end != -1:
return start_to_lever + lever_to_end
return -1
def short_distance(start, end, maps):
dp = [[-1 for _ in maps[0]] for _ in maps]
x, y = start
ex, ey = end
dp[x][y] = 0
directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]
queue = [start]
while queue:
curr = queue.pop(0)
x, y = curr
for d in directions:
nx, ny = x + d[0], y + d[1]
if not in_boundary((nx, ny), maps) or not can_go((nx, ny), maps):
continue
if dp[nx][ny] == -1 or dp[nx][ny] > dp[x][y] + 1:
queue.append((nx, ny))
dp[nx][ny] = dp[x][y] + 1
return dp[ex][ey]
def can_go(curr, maps):
return maps[curr[0]][curr[1]] != "X"
def in_boundary(curr, maps):
return 0 <= curr[0] < len(maps) and 0 <= curr[1] < len(maps[0])
def findXY(c, maps):
for i, line in enumerate(maps):
for j, sq in enumerate(line):
if sq == c:
return i, j