-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_matrix.cpp
54 lines (52 loc) · 1.52 KB
/
01_matrix.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
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
#include <fstream>
using namespace std;
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
vector<vector<int>> ans(matrix.size(), vector<int>(matrix[0].size(), 1e9));
vector<pair<int, int>> zeros;
vector<pair<int, int>> ones;
for (int i = 0; i < matrix.size(); ++i) {
for (int j = 0; j < matrix[i].size(); ++j) {
if (matrix[i][j] == 0) {
zeros.push_back(make_pair(i, j));
ans[i][j] = 0;
}
if (matrix[i][j] == 1) {
ones.push_back(make_pair(i, j));
}
}
}
for (int i = 0; i < ones.size(); ++i) {
for (int j = 0; j < zeros.size(); ++j) {
int d = abs(zeros[j].first - ones[i].first) + abs(zeros[j].second - ones[i].first);
ans[ones[i].first][ones[i].second] = min(ans[ones[i].first][ones[i].second], d);
}
}
return ans;
}
};
int main() {
Solution solution;
ifstream fin("input.txt");
int n;
cin >> n;
vector<vector<int>> v(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
fin >> v[i][j];
}
}
solution.updateMatrix(v);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << v[i][j] << " ";
}
cout << endl;
}
return 0;
}