Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int main() {
- int n, m;
- cin >> n >> m;
- int mat[n][m];
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- cin >> mat[i][j];
- }
- }
- int niza[n * m];
- int k = 0;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- int najmal_element = 2000000000;
- if(i + 1 < n) { // postoi dolniot sosed
- if(najmal_element > mat[i + 1][j]) {
- najmal_element = mat[i + 1][j];
- }
- }
- if(i - 1 >= 0) { // postoi gorniot sosed
- if(najmal_element > mat[i - 1][j]) {
- najmal_element = mat[i - 1][j];
- }
- }
- if(j + 1 < m) { // postoi desniot sosed
- if(najmal_element > mat[i][j + 1]) {
- najmal_element = mat[i][j + 1];
- }
- }
- if(j - 1 >= 0) { // postoi leviot sosed
- if(najmal_element > mat[i][j - 1]) {
- najmal_element = mat[i][j - 1];
- }
- }
- if(najmal_element > mat[i][j]) {
- niza[k] = najmal_element - mat[i][j];
- k++;
- }
- }
- }
- if(k == 0) {
- cout << -1 << endl;
- }
- else {
- sort(niza, niza + k);
- for(int i = k - 1; i >= 0; i--) {
- cout << niza[i] << " ";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement