Created
February 6, 2018 10:03
-
-
Save juanfal/c26053aa83b114d5eb2dd29c5f26f77e to your computer and use it in GitHub Desktop.
Print peaks of a matrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 01.peaks.cpp | |
// juanfc 2018-02-06 | |
#include <iostream> | |
#include <array> | |
using namespace std; | |
const int N = 3; | |
typedef array<int,N> TRow; | |
typedef array<TRow,N> TSqMat; | |
void printPeaks(TSqMat m); | |
int main() | |
{ | |
TSqMat a = {{ {{4, 5, 3}}, | |
{{6, 2, 2}}, | |
{{1, 8, 7}} }}, | |
b = {{ {{1, 2, 3}}, | |
{{4, 5, 6}}, | |
{{7, 8, 9}} }}, | |
c = {{ {{4, 4, 4}}, | |
{{4, 4, 4}}, | |
{{4, 4, 4}} }}, | |
d = {{ {{9, 8, 7}}, | |
{{6, 5, 4}}, | |
{{3, 2, 1}} }}; | |
printPeaks(a); cout << endl; | |
printPeaks(b); cout << endl; | |
printPeaks(c); cout << endl; | |
printPeaks(d); cout << endl; | |
return 0; | |
} | |
bool isPeak(TSqMat m, int i, int j); | |
void printPeaks(TSqMat m) | |
{ | |
for (int i = 0; i < N; ++i) | |
for (int j = 0; j < N; ++j) | |
if (isPeak(m, i, j) ) | |
cout << "Row " << i << " column " << j << ", value " << m[i][j] << endl; | |
} | |
bool isOut(int row, int col); | |
bool isPeak(TSqMat m, int i, int j) | |
{ | |
return | |
(isOut(i-1, j) or m[i-1][j] <= m[i][j]) and | |
(isOut(i+1, j) or m[i+1][j] <= m[i][j]) and | |
(isOut(i, j-1) or m[i][j-1] <= m[i][j]) and | |
(isOut(i, j+1) or m[i][j+1] <= m[i][j]); | |
} | |
bool isOut(int row, int col) | |
{ | |
return row < 0 or row >= N or col < 0 or col >= N; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment