-
Notifications
You must be signed in to change notification settings - Fork 15
/
kth-manhattan-distance-neighbourhood_test.cpp
84 lines (66 loc) · 2.25 KB
/
kth-manhattan-distance-neighbourhood_test.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) \
do { \
std::cout << #x << ": " << x << " "; \
} while (0)
#define DEBUGL(x) \
do { \
std::cout << #x << ": " << x << "\n"; \
} while (0)
#define MOD 1000000007
template <typename T> void print_vector(vector<T> a) {
for (auto &i : a) {
cout << i << " ";
}
cout << "\n";
}
template <typename T> void print_matrix_2d(vector<vector<T>> a) {
int r = 0;
for (auto &i : a) {
cout << r << ": ";
for (auto &j : i) {
cout << j << " ";
}
r++;
cout << "\n";
}
cout << "\n";
}
int max_of(int a, int b, int c, int d, int e) {
return max(max(max(a, b), max(c, d)), e);
}
// Time - O(N * K), Space - O(N)
// where N = size of 2d array, K = distance given
vector<vector<int>> solve(int A, const vector<vector<int>> &B) {
int rows = B.size();
int cols = B[0].size();
int max_distance = A;
vector<vector<int>> cur_distances = B;
vector<vector<int>> next_distances = cur_distances;
for (int dist = 0; dist < max_distance; dist++) {
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
int cur = cur_distances[r][c];
int right = (c == cols - 1) ? -1 : cur_distances[r][c + 1];
int up = (r == 0) ? -1 : cur_distances[r - 1][c];
int left = (c == 0) ? -1 : cur_distances[r][c - 1];
int down = (r == rows - 1) ? -1 : cur_distances[r + 1][c];
next_distances[r][c] = max_of(cur, right, up, left, down);
}
}
cur_distances = next_distances;
}
return cur_distances;
}
int main() {
// ios::sync_with_stdio(0);
// cin.tie(0);
vector<vector<int>> distances{
{1, 2, 4},
{4, 5, 8},
};
int max_distance = 2;
print_matrix_2d(solve(max_distance, distances));
return 0;
}