-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy path1986B-MatrixStabilization.cpp
36 lines (30 loc) · 1.31 KB
/
1986B-MatrixStabilization.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
#include <cstdio>
#include <vector>
int main(){
long t; scanf("%ld", &t);
while(t--){
long n, m; scanf("%ld %ld", &n, &m);
std::vector<std::vector<long> > v(n, std::vector<long>(m, 0));
for(long row = 0; row < n; row++){
for(long col = 0; col < m; col++){
scanf("%ld", &v[row][col]);
}
}
for(long row = 0; row < n; row++){
for(long col = 0; col < m; col++){
bool largest(true); long mx(0);
if(row > 0 && v[row - 1][col] >= v[row][col]){largest = false;}
else if(row > 0){mx = (mx > v[row - 1][col] ? mx : v[row - 1][col]);}
if(row + 1 < n && v[row + 1][col] >= v[row][col]){largest = false;}
else if(row + 1 < n){mx = (mx > v[row + 1][col] ? mx : v[row + 1][col]);}
if(col > 0 && v[row][col - 1] >= v[row][col]){largest = false;}
else if(col > 0){mx = (mx > v[row][col - 1] ? mx : v[row][col - 1]);}
if(col + 1 < m && v[row][col + 1] >= v[row][col]){largest = false;}
else if(col + 1 < m){mx = (mx > v[row][col + 1] ? mx : v[row][col + 1]);}
if(largest){v[row][col] = mx;}
printf("%ld ", v[row][col]);
}
puts("");
}
}
}