BOJ/C++
알고스팟
IamToday
2020. 2. 28. 19:27
*우선순위큐를 사용해서 더 적게 벽을 뿌순 순서대로 정렬하여 큐에서 꺼낸다.
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
70
71
72
73
74
75
|
#include <iostream>
#include <vector>
#include <queue>
#include <functional>
#define INF 987654321
using namespace std;
int n, m, ans = INF;
int map[101][101];
typedef pair<int, int> pp;
typedef pair<int ,pp> ipp;
bool visited[101][101];
int dx[4] = {-1,1,0,0}, dy[4] = {0,0,-1,1};
bool check(int x, int y) {
if (x < 0 || x >= n || y < 0 || y >= m || visited[x][y]) return false;
return true;
}
void bfs(int x, int y) {
priority_queue<ipp, vector<ipp>, greater<ipp> > q;
q.push(ipp(0, pp(x, y)));
visited[x][y] = true;
while(!q.empty()) {
int size = q.size();
while(size--) {
int cnt = q.top().first;
q.pop();
if (x == n-1 && y == m-1) {
ans = min(ans, cnt);
continue;
}
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (check(nx, ny)) {
visited[nx][ny] = true;
if (map[nx][ny])
q.push(ipp(cnt+1, pp(nx, ny)));
else q.push(ipp(cnt, pp(nx, ny)));
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.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';
}
}
bfs(0,0);
cout << ans << '\n';
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|