-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1261 알고스팟(우선순위 큐).cpp
58 lines (52 loc) · 1.15 KB
/
1261 알고스팟(우선순위 큐).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
#include<iostream>
#include<vector>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
int n, m;
int map[101][101];
bool visited[101][101];
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> q;
int bfs() {
q.push({ 0,{ 0 ,0 } });
visited[0][0] = 1;
while (q.size()) {
int y = q.top().second.first;
int x = q.top().second.second;
int block = q.top().first;
q.pop();
if (y == n - 1 && x == m - 1) {
return block;
}
for (int i = 0; i < 4; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
if (nx<0 || nx>m - 1 || ny<0 || ny>n - 1) continue;
if (!visited[ny][nx]) {
if (map[ny][nx] == 0) {
q.push({ block,{ ny, nx } });
}
else {
q.push({ block + 1,{ ny, nx } });
}
visited[ny][nx] = 1;
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> m >> n;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int j = 0; j < m; j++) {
map[i][j] = s[j] - '0';
}
}
cout << bfs() << '\n';
}