Skip to content

Commit f2d7a9a

Browse files
committed
[Gold III] Title: 벽 부수고 이동하기, Time: 872 ms, Memory: 113408 KB -BaekjoonHub
1 parent 69ff7b7 commit f2d7a9a

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [Gold III] 벽 부수고 이동하기 - 2206
2+
3+
[문제 링크](https://www.acmicpc.net/problem/2206)
4+
5+
### 성능 요약
6+
7+
메모리: 113408 KB, 시간: 872 ms
8+
9+
### 분류
10+
11+
그래프 이론, 그래프 탐색, 너비 우선 탐색, 격자 그래프
12+
13+
### 제출 일자
14+
15+
2026년 2월 27일 15:29:51
16+
17+
### 문제 설명
18+
19+
<p>N×M의 행렬로 표현되는 맵이 있다. 맵에서 0은 이동할 수 있는 곳을 나타내고, 1은 이동할 수 없는 벽이 있는 곳을 나타낸다. 당신은 (1, 1)에서 (N, M)의 위치까지 이동하려 하는데, 이때 최단 경로로 이동하려 한다. 최단경로는 맵에서 가장 적은 개수의 칸을 지나는 경로를 말하는데, 이때 시작하는 칸과 끝나는 칸도 포함해서 센다.</p>
20+
21+
<p>만약에 이동하는 도중에 한 개의 벽을 부수고 이동하는 것이 좀 더 경로가 짧아진다면, 벽을 한 개 까지 부수고 이동하여도 된다.</p>
22+
23+
<p>한 칸에서 이동할 수 있는 칸은 상하좌우로 인접한 칸이다.</p>
24+
25+
<p>맵이 주어졌을 때, 최단 경로를 구해 내는 프로그램을 작성하시오.</p>
26+
27+
### 입력
28+
29+
<p>첫째 줄에 N(1 ≤ N ≤ 1,000), M(1 ≤ M ≤ 1,000)이 주어진다. 다음 N개의 줄에 M개의 숫자로 맵이 주어진다. (1, 1)과 (N, M)은 항상 0이라고 가정하자.</p>
30+
31+
### 출력
32+
33+
<p>첫째 줄에 최단 거리를 출력한다. 불가능할 때는 -1을 출력한다.</p>
34+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.util.Deque;
2+
import java.util.ArrayDeque;
3+
import java.util.Scanner;
4+
5+
class Main {
6+
7+
static int[] dx = {-1, 1, 0, 0};
8+
static int[] dy = {0, 0, -1, 1};
9+
10+
static boolean[][][] visited;
11+
12+
public static void main(String[] args) {
13+
Scanner sc = new Scanner(System.in);
14+
String[] nAndM = sc.nextLine().trim().split(" ");
15+
int N = Integer.parseInt(nAndM[0]);
16+
int M = Integer.parseInt(nAndM[1]);
17+
18+
int[][] mapInfo = new int[N][M];
19+
20+
for (int i = 0; i < N; i++) {
21+
String line = sc.nextLine().trim();
22+
for (int j = 0; j < M; j++) {
23+
mapInfo[i][j] = line.charAt(j) - '0';
24+
}
25+
}
26+
27+
System.out.println(findShortestRoute(mapInfo));
28+
}
29+
30+
private static int findShortestRoute(int[][] maps) {
31+
int N = maps.length;
32+
int M = maps[0].length;
33+
34+
visited = new boolean[N][M][2];
35+
Deque<int[]> queue = new ArrayDeque<>();
36+
37+
queue.offer(new int[]{0, 0, 1, 0});
38+
visited[0][0][0] = true;
39+
40+
while (!queue.isEmpty()) {
41+
int[] currentPosition = queue.poll();
42+
43+
if (currentPosition[0] == N - 1 && currentPosition[1] == M - 1) {
44+
return currentPosition[2];
45+
}
46+
47+
for (int i = 0; i < 4; i++) {
48+
int nX = currentPosition[0] + dx[i];
49+
int nY = currentPosition[1] + dy[i];
50+
51+
if (nX >= 0 && nX < maps.length && nY >= 0 && nY < maps[0].length) {
52+
if (maps[nX][nY] == 0 && !visited[nX][nY][currentPosition[3]]) {
53+
visited[nX][nY][currentPosition[3]] = true;
54+
queue.offer(new int[]{nX, nY, currentPosition[2] + 1, currentPosition[3]});
55+
} else if (maps[nX][nY] == 1 && currentPosition[3] == 0 && !visited[nX][nY][1]) {
56+
visited[nX][nY][1] = true;
57+
queue.offer(new int[]{nX, nY, currentPosition[2] + 1, 1});
58+
}
59+
}
60+
}
61+
}
62+
63+
return -1;
64+
}
65+
}

0 commit comments

Comments
 (0)