-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14940.cpp
57 lines (51 loc) · 1.39 KB
/
14940.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
#include <iostream>
#include <queue>
#include <cstring>
#include <utility>
#include <functional>
#include <algorithm>
#include <vector>
using namespace std;
const int INF = 1000000000;
int dx[] = { -1, 0, 1, 0 };
int dy[] = { 0, -1, 0, 1 };
int n, m;
int arr[1001][1001], dist[1001][1001];
int sr, sc;
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
dist[i][j] = INF;
scanf("%d", &arr[i][j]);
if (arr[i][j] == 2) {
sr = i; sc = j;
dist[i][j] = 0;
}
else if (arr[i][j] == 0)
dist[i][j] = 0;
}
}
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
pq.push(pair<int, pair<int, int> >(0, pair<int, int>(sr, sc)));
while (!pq.empty()) {
int cr = pq.top().second.first, cc = pq.top().second.second, cd = pq.top().first;
pq.pop();
if (cd > dist[cr][cc]) continue;
for (int i = 0; i < 4; i++) {
int nextr = cr + dy[i], nextc = cc + dx[i];
if (0 <= nextr && nextr < n && 0 <= nextc && nextc < m && arr[nextr][nextc] == 1) {
if (dist[nextr][nextc] > dist[cr][cc] + 1) {
dist[nextr][nextc] = dist[cr][cc] + 1;
pq.push(pair<int, pair<int, int> >(dist[cr][cc] + 1, pair<int, int>(nextr, nextc)));
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
printf("%d ", dist[i][j] < INF ? dist[i][j] : -1);
printf("\n");
}
return 0;
}