-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaekJoon2206.cpp
69 lines (59 loc) · 1.32 KB
/
BaekJoon2206.cpp
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
65
66
67
68
69
//BaekJoon 2206 벽 부수고 이동하기
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <utility>
using namespace std;
const int MAX = 1001;
int n, m;
int dx[] = { 0, 1, 0, -1 };
int dy[] = { -1, 0, 1, 0 };
int arr[MAX][MAX] = { 0, };
bool visited[MAX][MAX][2];
struct Node {
int x;
int y;
int distance;
int wallDestroyed;
};
queue<Node> qu;
int bfs() {
if (n == 1 && m == 1) return 1;
qu.push({ 0,0,1,0 });
visited[0][0][0] = true;
while (!qu.empty()) {
Node node = qu.front();
qu.pop();
if (node.x == n - 1 && node.y == m - 1) {
return node.distance;
}
for (int i = 0; i < 4; i++) {
int x = node.x + dx[i];
int y = node.y + dy[i];
if (x < 0 || y < 0 || x >= n || y >= m) continue;
if (arr[x][y] == 0 && visited[x][y][node.wallDestroyed] == false) {
qu.push({ x, y, node.distance + 1, node.wallDestroyed });
visited[x][y][node.wallDestroyed] = true;
}
else if (arr[x][y] == 1 && node.wallDestroyed == 0) {
qu.push({ x,y,node.distance + 1, node.wallDestroyed + 1 });
visited[x][y][node.wallDestroyed + 1] = true;
}
}
}
return -1;
}
int main(void)
{
//ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%1d", &arr[i][j]);
}
}
cout << bfs();
return 0;
}