Skip to content

Commit 74c9f0a

Browse files
committed
[Silver I] Title: 미로 탐색, Time: 196 ms, Memory: 18840 KB -BaekjoonHub
1 parent 095fef3 commit 74c9f0a

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# [Silver I] 미로 탐색 - 2178
2+
3+
[문제 링크](https://www.acmicpc.net/problem/2178)
4+
5+
### 성능 요약
6+
7+
메모리: 18840 KB, 시간: 196 ms
8+
9+
### 분류
10+
11+
그래프 이론, 그래프 탐색, 너비 우선 탐색, 격자 그래프
12+
13+
### 제출 일자
14+
15+
2026년 2월 19일 23:50:46
16+
17+
### 문제 설명
18+
19+
<p>N×M크기의 배열로 표현되는 미로가 있다.</p>
20+
21+
<table class="table table-bordered" style="width:18%">
22+
<tbody>
23+
<tr>
24+
<td style="width:3%">1</td>
25+
<td style="width:3%">0</td>
26+
<td style="width:3%">1</td>
27+
<td style="width:3%">1</td>
28+
<td style="width:3%">1</td>
29+
<td style="width:3%">1</td>
30+
</tr>
31+
<tr>
32+
<td>1</td>
33+
<td>0</td>
34+
<td>1</td>
35+
<td>0</td>
36+
<td>1</td>
37+
<td>0</td>
38+
</tr>
39+
<tr>
40+
<td>1</td>
41+
<td>0</td>
42+
<td>1</td>
43+
<td>0</td>
44+
<td>1</td>
45+
<td>1</td>
46+
</tr>
47+
<tr>
48+
<td>1</td>
49+
<td>1</td>
50+
<td>1</td>
51+
<td>0</td>
52+
<td>1</td>
53+
<td>1</td>
54+
</tr>
55+
</tbody>
56+
</table>
57+
58+
<p>미로에서 1은 이동할 수 있는 칸을 나타내고, 0은 이동할 수 없는 칸을 나타낸다. 이러한 미로가 주어졌을 때, (1, 1)에서 출발하여 (N, M)의 위치로 이동할 때 지나야 하는 최소의 칸 수를 구하는 프로그램을 작성하시오. 한 칸에서 다른 칸으로 이동할 때, 서로 인접한 칸으로만 이동할 수 있다.</p>
59+
60+
<p>위의 예에서는 15칸을 지나야 (N, M)의 위치로 이동할 수 있다. 칸을 셀 때에는 시작 위치와 도착 위치도 포함한다.</p>
61+
62+
### 입력
63+
64+
<p>첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 <strong>붙어서</strong> 입력으로 주어진다.</p>
65+
66+
### 출력
67+
68+
<p>첫째 줄에 지나야 하는 최소의 칸 수를 출력한다. 항상 도착위치로 이동할 수 있는 경우만 입력으로 주어진다.</p>
69+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.Scanner;
2+
import java.util.List;
3+
import java.util.Deque;
4+
import java.util.ArrayDeque;
5+
6+
public class Main {
7+
8+
static int dx[] = {1, -1, 0, 0};
9+
static int dy[] = {0, 0, -1, 1};
10+
static int N;
11+
static int M;
12+
static int[][] maze;
13+
static boolean[][] visited;
14+
15+
static void bfs(int x, int y) {
16+
Deque<int[]> queue = new ArrayDeque<>();
17+
queue.offer(new int[]{x, y});
18+
19+
while (!queue.isEmpty()) {
20+
int[] current = queue.poll();
21+
22+
for (int i = 0; i < 4; i++) {
23+
int nextX = current[0] + dx[i];
24+
int nextY = current[1] + dy[i];
25+
26+
if (nextX >= 0 && nextX < N && nextY >= 0 && nextY < M) {
27+
if (!visited[nextX][nextY] && maze[nextX][nextY] == 1) {
28+
visited[nextX][nextY] = true;
29+
queue.offer(new int[]{nextX, nextY});
30+
maze[nextX][nextY] = maze[current[0]][current[1]] + 1;
31+
}
32+
}
33+
}
34+
}
35+
}
36+
37+
public static void main(String[] args) {
38+
Scanner sc = new Scanner(System.in);
39+
String[] input1 = sc.nextLine().split(" ");
40+
N = Integer.parseInt(input1[0]);
41+
M = Integer.parseInt(input1[1]);
42+
43+
maze = new int[N][M];
44+
visited = new boolean[N][M];
45+
46+
for (int i = 0; i < N; i++) {
47+
String mazeInput = sc.next();
48+
for (int j = 0; j < M; j++) {
49+
maze[i][j] = mazeInput.charAt(j) - '0';
50+
}
51+
}
52+
53+
visited[0][0] = true;
54+
bfs(0, 0);
55+
System.out.println(maze[N - 1][M - 1]);
56+
}
57+
}

0 commit comments

Comments
 (0)