I am trying to find out the adjacent cells of a cell (i,j); which will be (i,j+1), (i,j-1), (i-1,j) and (i+1,j) in a 2D array.
My code to implement this:
for i in range(len(array)):
for j in range(len(array)):
if i==0:
adjacent_row=[i,i+1]
elif i==len(array)-1:
adjacent_row=[i,i-1]
else:
adjacent_row=[i-1,i,i+1]
if j==0:
adjacent_column=[j]
elif j==len(array)-1: # The column part is actually wrong, I don't know how to fix it
adjacent_column=[j-1,j]
else:
adjacent_column=[j-1,j+1]
for k in adjacent_row:
for l in adjacent_column:
#traverse through those cells
This was the idea that I had in my mind, but it's flawed.