I'm creating a depth first search program which searches through a 2d array to find the number 1 and always starts at 0. I'm having some trouble with finding the neighbours of each element in the array, I have a method (based off the pseudocode found here Finding neighbours in a two-dimensional array):
private static void findNeighbour(Integer[][] maze) {
int row = 0;
int col = 0;
int row_limit = maze.length;
if(row_limit > 0){
int column_limit = maze[0].length;
for(int y = Math.max(0, col-1); y <= Math.min(col+1, column_limit); y++){
for(int x = Math.max(0, row-1); x <= Math.min(row+1, row_limit); x++){
if(x != row || y != col){
// printArray(maze);
neighbours.push(x);
neighbours.push(y);
}
}
}
}
}
Essentially I am trying to go through the 2d array, find each neighbour then place the neighbours into a stack so I can pop them off the stack in the dfs. I've put the maze I am using below along with my output I am currently getting. I would greatly appreciate it if anyone could point me in the right direction/point out anything that seems to be causing it not to find the neighbours.
maze:
static Integer[][] maze = { { 11, 3 }, { 2, 3 }, { 0, 3 }, { 1, 4 }, { 5, 4 }, { 5, 7 }, { 6, 7 }, { 7, 8 }, { 8, 9 },
{ 9, 10 }, { 0, 5 } };
output:
[1, 0, 0, 1, 1, 1]