-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmopc21c6p3.cpp
53 lines (51 loc) · 1.39 KB
/
dmopc21c6p3.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
#include <bits/stdc++.h>
using namespace std;
int adj[1501][1501];
int dis[1501][1501];
vector<pair<int, pair<int, int> > > v;
queue<pair<int, int> > q;
bool valid (int x, int y, int w, int h){
return (x >= 1 && x <= w && y >= 1 && y <= h);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
memset(dis, 0, sizeof(dis));
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
cin >> adj[i][j];
if (adj[i][j] != 0) {
pair<int, int> curr = make_pair(i,j);
v.push_back(make_pair(adj[i][j], curr));
}
}
}
sort(v.begin(), v.end());
for (auto i: v){
q.push(i.second);
}
int dir[4][2] = {{-1, 0}, {1,0}, {0, -1}, {0,1}};
pair<int, int> curr;
while (!q.empty()){
curr = q.front();
q.pop();
int r = curr.first, c = curr.second;
if (dis[r][c] == k) continue;
for (int i = 0; i < 4; i++){
int x = r + dir[i][0], y = c + dir[i][1];
if (adj[x][y] == 0 && valid (x, y, n, m)){
adj[x][y] = adj[r][c];
q.push(make_pair(x,y));
dis[x][y] = dis[r][c]+1;
}
}
}
for (int i = 1; i<= n; i++){
for (int j = 1; j <= m; j++){
cout << adj[i][j] << " ";
}
cout << "\n";
}
}