0

I have now created an 8x8 maze search algorithm using a recursive algorithm. I have no idea what the problem is with the array. but I can not find the cause of this error in "main".

public class Maze {
private static int N=8;
private static int [][] maze = {
        {0,0,0,0,0,0,0,1}, // 미로 라고 봄
        {0,1,1,0,1,1,0,1},
        {0,0,0,1,0,0,0,1},
        {0,1,0,0,1,1,0,0},
        {0,1,1,1,0,0,1,1},
        {0,1,0,0,0,1,0,1},
        {0,0,0,1,0,0,0,1},
        {0,1,1,1,0,1,0,0}
};

private static final int PATHWAY_COLOUR = 0; // white
private static final int WALL_COLOUR = 1; // blue
private static final int BLOCKED_COLOUR = 2; // red
private static final int PATH_COLOUR = 3; // green

public static boolean findMazePath(int x, int y){
    if (x<0 || y<0 || x>=N) // 유효한 좌표 범위 확인
        return false;
    else if (maze[x][y] != PATHWAY_COLOUR)
        return false;
    else if (x==N-1 && y==N-1){ // 출구
        maze[x][y] = PATH_COLOUR;
        return true;
    }
    else {
        maze[x][y] = PATH_COLOUR;
        if (findMazePath(x-1,y) || findMazePath(x,y+1)
                ||findMazePath(x+1,y) || findMazePath(x,y-1)){
            return true;
        } // 북 동 남 서 방향 순으로 확인
        maze[x][y] = BLOCKED_COLOUR; // dead end
        return false;
    }
}

public static void main(String[] args) {
//      printMaze();
    findMazePath(0,0); // 시작
//      printMaze();
    }
}
Taehyeon
  • 31
  • 8

2 Answers2

0

in your preconditions you are checking against x < 0 and y < 0 and x >= N but you are not checking against y >= N.

Mike Nakis
  • 46,450
  • 8
  • 79
  • 117
0

You are missing Y check in line number 22 , condition should be

if (x<0 || y<0 || x>=N || y>=N) // 유효한 좌표 범위 확인
Kushagra Misra
  • 392
  • 4
  • 13