-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ_2206_벽부수고이동하기.cpp
More file actions
64 lines (58 loc) · 1.85 KB
/
BJ_2206_벽부수고이동하기.cpp
File metadata and controls
64 lines (58 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <queue>
using namespace std;
int maze[1000][1000];
bool visited[1000][1000][2] = {false};
int BFS(int N, int M);
int main() {
int i, j, count = -1;
int N, M;
cin >> N >> M;
// 1. ÀÔ·Â
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++)
scanf("%1d", &maze[i][j]); }
// 2. ã±â
count = BFS(N, M);
// Ãâ·Â
cout << count;
}
int BFS(int N, int M) {
int x, y, move, punch;
queue < pair<pair<int, int>, pair<int, int>>> exit;
exit.push(make_pair(make_pair(0, 0), make_pair(1, 0)));
visited[0][0][0] = true;
while (!exit.empty()) {
x = exit.front().first.first;
y = exit.front().first.second;
move = exit.front().second.first;
punch = exit.front().second.second;
exit.pop();
if (x == N - 1 && y == M - 1) return move;
if (x - 1 >= 0 && !visited[x - 1][y][punch] && (maze[x - 1][y] == 0 || punch == 0)) {
int pn = punch;
if (maze[x - 1][y] == 1 && punch == 0) { pn = punch + 1; }
exit.push(make_pair(make_pair(x - 1, y), make_pair(move + 1, pn)));
visited[x - 1][y][pn] = true;
}
if (x + 1 < N && !visited[x + 1][y][punch] && (maze[x + 1][y] == 0 || punch == 0)) {
int pn = punch;
if (maze[x + 1][y] == 1 && punch == 0) { pn = punch + 1; }
exit.push(make_pair(make_pair(x + 1, y), make_pair(move + 1, pn)));
visited[x + 1][y][pn] = true;
}
if (y - 1 >= 0 && !visited[x][y - 1][punch] && (maze[x][y - 1] == 0 || punch == 0)) {
int pn = punch;
if (maze[x][y - 1] == 1 && punch == 0) { pn = punch + 1; }
exit.push(make_pair(make_pair(x, y - 1), make_pair(move + 1, pn)));
visited[x][y - 1][pn] = true;
}
if (y + 1 < M && !visited[x][y + 1][punch] && (maze[x][y + 1] == 0 || punch == 0)) {
int pn = punch;
if (maze[x][y + 1] == 1 && punch == 0) { pn = punch + 1; }
exit.push(make_pair(make_pair(x, y + 1), make_pair(move + 1, pn)));
visited[x][y + 1][pn] = true;
}
}
return -1;
}