-
-
Save CarlosRA97/f30729cd1ae62e72866940c2ec2b41e1 to your computer and use it in GitHub Desktop.
Print peaks of a matrix
This file contains 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