Given a n*n matrix and a value k, how do we find all the neighbors for each element?
for example: in a 4*4
matrix, with k=2
say matrix is :
[ 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16]
where these values are the indexes of the location, the neighbors for 1 are 1,2,3,5,6,9
. The values 3,6 and 9
come only because k =2 and wouldnt be there if k was = 1.
similarly the neighbors of 6 will be 1 2 3 5 6 7 8 9 10 11 and 14
Can you please help me to write a c code to implement this in c++.
It is the problem of von Neumann neighborhood, please can some one implement it in c++. Thanks