๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/6593)
< ์๋ฒ ๋น๋ฉ >
๋ฌธ์ ํ์ด
3์ฐจ์์ผ๋ก ์ ๋ ฅ๋ฐ์ ํ ์, ํ, ์ข, ์ฐ, ์, ์๋๋ฅผ ํ์ํ๋ฉฐ ๋น์ด์๋ ์นธ์ผ๋ก ์ด๋ํ๋ฉฐ
์ถ๊ตฌ๋ฅผ ๋ง๋ ๊ฒฝ์ฐ ์ข ๋ฃํด ์ฃผ์๋ค.
- my solution (Java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class _6593_ {
static int[] dx = { -1, 1, 0, 0 };
static int[] dy = { 0, 0, -1, 1 };
static Queue<int[]> queue;
static char[][][] map;
static int visited[][][], L, R, C, result;
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
queue = new LinkedList<>();
while (true) {
st = new StringTokenizer(bf.readLine());
L = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
if (L == 0)
break;
result = 0;
map = new char[L][R][C];
visited = new int[L][R][C];
for (int i = 0; i < L; i++) {
for (int j = 0; j < R; j++) {
st = new StringTokenizer(bf.readLine());
String line = st.nextToken();
for (int k = 0; k < C; k++) {
map[i][j][k] = line.charAt(k);
if (map[i][j][k] == 'S')
queue.add(new int[] { i, j, k });
}
}
new StringTokenizer(bf.readLine());
}
search();
queue.clear();
if(result>0) System.out.println("Escaped in "+result+" minute(s).");
else System.out.println("Trapped!");
}
}
private static void search() {
while (!queue.isEmpty()) {
int[] temp = queue.poll();
if (temp[0] - 1 >= 0) {
if (map[temp[0] - 1][temp[1]][temp[2]] == 'E') {
result = visited[temp[0]][temp[1]][temp[2]] + 1;
break;
} else if (map[temp[0] - 1][temp[1]][temp[2]] == '.' && visited[temp[0] - 1][temp[1]][temp[2]] == 0) {
visited[temp[0] - 1][temp[1]][temp[2]] = visited[temp[0]][temp[1]][temp[2]] + 1;
queue.add(new int[] { temp[0] - 1, temp[1], temp[2] });
}
}
if (temp[0] + 1 < L) {
if(map[temp[0]+1][temp[1]][temp[2]]=='E') {
result = visited[temp[0]][temp[1]][temp[2]] + 1;
break;
}
if (map[temp[0] + 1][temp[1]][temp[2]] == '.' && visited[temp[0] + 1][temp[1]][temp[2]] == 0) {
visited[temp[0] + 1][temp[1]][temp[2]] = visited[temp[0]][temp[1]][temp[2]] + 1;
queue.add(new int[] { temp[0] + 1, temp[1], temp[2] });
}
}
for (int i = 0; i < 4; i++) {
int x = temp[1] + dx[i];
int y = temp[2] + dy[i];
if (x >= 0 && x < R && y >= 0 && y < C) {
if(map[temp[0]][x][y]=='E') {
result = visited[temp[0]][temp[1]][temp[2]] + 1;
break;
}
if (map[temp[0]][x][y] == '.' && visited[temp[0]][x][y] == 0) {
visited[temp[0]][x][y] = visited[temp[0]][temp[1]][temp[2]] + 1;
queue.add(new int[] { temp[0], x, y });
}
}
}
if(result>0)break;
}
}
}
Main
- L, R, C ์ ๋ ฅ
- ์ ๋ ฅ์ ๋์ด L, R, C๊ฐ ๋ชจ๋ 0์ผ๋ก ํํ๋๋ฏ๋ก L์ด 0์ธ ๊ฒฝ์ฐ ์ข ๋ฃ
- result, map, visited๋ฅผ ์ด๊ธฐํ
- map ์ ๋ ฅ๋ฐ์ผ๋ฉฐ ์์ ์ง์ ์ 'S'๋ฅผ ์ ๋ ฅ๋ฐ์ผ๋ฉด queue์ ๋ฃ์ด์ค
- search ํจ์ ํธ์ถ
- ๋ค์ ์ฐ์ฐ์ ์ํด queue ์ด๊ธฐํํด ์ค
- ๋ต ์ถ๋ ฅ
search()
- queue์์ ํ๋ ๋ฝ์
- ์์ผ๋ก ์ด๋ํ์ฌ ๋น๋ฉ ๋ฒ์ ์์ด๋ฉฐ ๋น์ด์๋ ์นธ์ด๊ณ ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ๊ณณ์ด๋ฉด ์ด๋
- ํ๋ก ์ด๋ํ์ฌ ๋น๋ฉ ๋ฒ์ ์์ด๋ฉฐ ๋น์ด์๋ ์นธ์ด๊ณ ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ๊ณณ์ด๋ฉด ์ด๋
- ๋, ์, ๋จ, ๋ถ์ผ๋ก ์ด๋ํ์ฌ ๋น๋ฉ ๋ฒ์ ์์ด๋ฉฐ ๋น์ด์๋ ์นธ์ด๊ณ ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ๊ณณ์ด๋ฉด ์ด๋
- ๊ฐ ์ด๋ํ ๊ณณ์ด ๋น ๊ณณ์ด ์๋๋ผ ์ถ๊ตฌ์ธ 'E'์ด๋ผ๋ฉด ์ข ๋ฃ
์๊ฐ๐ค
3์ฐจ์์ธ ๊ฒ๋ง ๊ณ ๋ คํ๋ค๋ฉด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์๋ค.
'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 3187_์์น๊ธฐ ๊ฟ (0) | 2023.02.22 |
---|---|
[Baekjoon] 1240_๋ ธ๋์ฌ์ด์ ๊ฑฐ๋ฆฌ (0) | 2023.01.15 |
[Baekjoon] 13565_์นจํฌ (1) | 2022.12.27 |
[Baekjoon] 16197_๋ ๋์ (0) | 2022.12.26 |
[Baekjoon] 9019_DSLR (0) | 2022.12.23 |