Skip to content

Commit 5a58313

Browse files
committed
[Gold III] Title: 미로 탈출, Time: 192 ms, Memory: 23836 KB -BaekjoonHub
1 parent 4b45844 commit 5a58313

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# [Gold III] 미로 탈출 - 14923
2+
3+
[문제 링크](https://www.acmicpc.net/problem/14923)
4+
5+
### 성능 요약
6+
7+
메모리: 23836 KB, 시간: 192 ms
8+
9+
### 분류
10+
11+
그래프 이론, 그래프 탐색, 너비 우선 탐색
12+
13+
### 제출 일자
14+
15+
2025년 10월 10일 20:38:36
16+
17+
### 문제 설명
18+
19+
<p>홍익이는 사악한 마법사의 꾐에 속아 N x M 미로 (Hx, Hy) 위치에 떨어졌다. 다행히도 홍익이는 마법사가 만든 미로의 탈출 위치(Ex, Ey)를 알고 있다. 하지만 미로에는 곳곳에 마법사가 설치한 벽이 있어 홍익이가 탈출하기 어렵게 하고 있다.</p>
20+
21+
<p>홍익이는 마법사의 연구실에서 훔친 지팡이가 있어, 벽을 길로 만들 수 있다. 그렇지만, 안타깝게도 마법의 지팡이는 단 한 번만 사용할 수 있다.</p>
22+
23+
<p>이때, 홍익이를 도와 미로에서 탈출할 수 있는지 알아보고, 할 수 있다면 가장 빠른 경로의 거리 D는 얼마인지 알아보자.</p>
24+
25+
<p>인접한 칸으로 이동하는데 똑같은 시간이 들고, 벽을 부수는 데 시간이 걸리지 않는다.</p>
26+
27+
### 입력
28+
29+
<pre>N M
30+
Hx Hy
31+
Ex Ey
32+
N X M 행렬</pre>
33+
34+
<ul>
35+
<li>2 ≤ N ≤ 1000, 2 ≤ M ≤ 1000</li>
36+
<li>1 ≤ Hx, Hy, Ex, Ey ≤ 1000</li>
37+
<li>(Hx, Hy)≠ (Ex, Ey)</li>
38+
<li>행렬은 0과 1로만 이루어져 있고, 0이 빈 칸, 1이 벽이다.</li>
39+
</ul>
40+
41+
### 출력
42+
43+
<p>D (탈출 할 수 없다면, -1을 출력한다.)</p>
44+
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
#include <tuple>
5+
#include <cstring>
6+
7+
using namespace std;
8+
9+
#define MAX 1001
10+
11+
int N, M;
12+
int H_x, H_y;
13+
int E_x, E_y;
14+
int maps[MAX][MAX] {};
15+
int new_maps[MAX][MAX] {};
16+
vector<pair<int, int>> walls;
17+
int dx[4] = {0, 0, -1, 1};
18+
int dy[4] = {1, -1, 0, 0};
19+
int visited[MAX][MAX][2] {};
20+
int answer = 1000001;
21+
22+
void input() {
23+
cin >> N >> M;
24+
cin >> H_x >> H_y;
25+
cin >> E_x >> E_y;
26+
for (int i = 1; i <= N; i ++) {
27+
for (int j = 1; j <= M; j++) {
28+
cin >> maps[i][j];
29+
if (maps[i][j] == 1) {
30+
walls.emplace_back(i, j);
31+
}
32+
}
33+
}
34+
}
35+
36+
void old_to_new_maps() {
37+
for (int i = 1; i <= N; i ++) {
38+
for (int j = 1; j <= M; j++) {
39+
new_maps[i][j] = maps[i][j];
40+
}
41+
}
42+
}
43+
44+
void bfs() {
45+
queue<pair<pair<int, int>, pair<int, int>>> q;
46+
q.push({{H_x, H_y}, {0, 1}});
47+
48+
while (!q.empty()) {
49+
int cx = q.front().first.first;
50+
int cy = q.front().first.second;
51+
int dist = q.front().second.first;
52+
int key = q.front().second.second;
53+
q.pop();
54+
55+
if (E_x == cx && E_y == cy) {
56+
cout << dist;
57+
return;
58+
}
59+
if (visited[cx][cy][key]) continue;
60+
visited[cx][cy][key] = 1;
61+
62+
for (int i = 0; i < 4; i++) {
63+
int nx = cx + dx[i];
64+
int ny = cy + dy[i];
65+
66+
if (nx <= 0 || ny <= 0 || nx > N || ny > M) continue;
67+
if (maps[nx][ny] == 1 && key == 1) q.push({{nx, ny}, {dist + 1, 0}});
68+
if (maps[nx][ny] == 0) q.push({{nx, ny}, {dist + 1, key}});
69+
}
70+
}
71+
72+
cout << "-1";
73+
}
74+
75+
int main() {
76+
input();
77+
bfs();
78+
return 0;
79+
}

0 commit comments

Comments
 (0)