728x90
반응형
public class MyClass {
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 void main(String[] args) {
findMazePath(0,0);
printMaze();
}
public static boolean findMazePath(int x, int y){
if(x<0 || y<0 || x>=N || y >= 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 printMaze() {
for(int i=0; i<N;i++){
for(int j=0; j<N; j++){
System.out.println(maze[i][j]);
}
}
}
}
728x90
반응형
'알고리즘' 카테고리의 다른 글
[재귀함수/backtracking] n queens problem (0) | 2021.12.02 |
---|---|
[재귀함수] Counting Cells in a Blob (0) | 2021.11.30 |
[프로그래머스/JAVA] 프린터 (0) | 2021.11.16 |
[프로그래머스/C++] 베스트앨범 (0) | 2021.10.13 |
[C++] vector, map (0) | 2021.10.06 |