I am trying to create an algorithm using the straightforward approach to find all peaks in a N x N matrix. But I am facing some issues with the corners, the first and last row, and first and last column. I'm considering the problem as it follows:
[ ][c][ ][ ] a is considered a 2d peak
[d][a][e][ ] or a hill iff a >= b,
[ ][b][ ][ ] a >= d, a >=c, a >=e
[ ][ ][ ][ ]
However, when I need to evaluate the corners as the representation bellow, c and d don't exist. I have to evaluate too many conditions in order to get to something more generic. i.e, for the a bellow, I'll need to check if the position a is valid if row-1 < 0, then I wouldn't need to check anything above a and if col-1 < 0 anything is in the left side of a.
[a][e][ ][ ]
[b][ ][ ][ ]
[ ][ ][ ][ ]
[ ][ ][ ][ ]
But when we take a to the other corners or even evaluating the other positions such as e in the example above, I need to certify myself if e is in a valid position where row-1 exists to prevent evaluating that position and get an error (in the example above I'd surely get an error checking if e >= the element in row-1. I've implemented a piece of code considering the corners, lines 0 and n and columns 0 and n but I stopped and started thinking on how to make it more readable and simple.
void find_hill(){
int i, j;
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
//check corners
if(i-1 < 0){
if(j-1 < 0){
//check if arr[i][j] >= arr at i,j+1 and i+1,j
}
if(j+1 > n-1){
//check if arr[i][j] >= arr at i,j-1 and i+1,j
}
}
}
}
}
I would like to discuss how could I come up to a solution here. Where should I start from to get something simple? I've thought somehow using an approach similar to the flood fill, but it fills each position separately, probably it wouldn't work since I'd end up in the same problem!