// 파일명 : ./Chapter21/MazeMove.java
import java.util.Scanner;
public class MazeMove
{
private static int[][] map = {
{ 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 1, 1 },
{ 1, 1, 1, 0, 1, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 2 },
{ 1, 1, 1, 1, 1, 1, 1 }
};
private static final int HEIGHT = 6, WIDTH = 7;
// 미로 맵 출력
private static int previousRow = -1, previousColumn = -1;
private static void printMap( int row, int column ) {
// 현재 위치가 직전 위치와 동일하면 출력 생략
if ( ( row == previousRow ) && ( column == previousColumn ) )
return;
// 길, 벽, 문, 캐릭터의 기호를 바탕으로 출력
String[] symbol = { " ", " \033[44m \033[0m", " \033[34m문 \033[0m", " " };
for( int i = 0; i < HEIGHT; i++ ) {
for( int j = 0; j < WIDTH; j++ ) {
if( ( i == row ) && ( j == column ) )
System.out.print( "옷" );
else
System.out.print( symbol[map[i ][j]] );
}
System.out.println();
}
System.out.println();
previousRow = row;
previousColumn = column;
}
private static final int SUCCESS = 2;
private static final int VISITED = 3;
private static final int NOTVISITED = 0;
// 미로 맵에서 길 찾는 함수
Fb private static int move( int row, int column ) {
// 먼저 현재 위치를 확인
F1 if ( ( row < 0 ) || ( HEIGHT <= row ) || ( column < 0 ) || ( WIDTH <= column ) )
F2 return 0;
// 현재 위치가 목표 지점이면 길 찾기 종료
F3 else if ( map[row ][column]== SUCCESS ) {
printMap( row, column );
F4 return SUCCESS;
}
// 현재 위치가 아직 지나가지 않은 길이면 지나간 흔적을 남기기
F5 else if ( map[row ][column]== NOTVISITED ) {
F6 map[row ][column]= VISITED;
// 목표 지점에 도착할 때까지 상하좌우로 한 칸씩 이동해 보기
printMap( row, column );
F7 if ( move( row + 1, column )
F8 == SUCCESS )
F9 return SUCCESS;
printMap( row, column );
F10 if ( move( row, column + 1 )
F11 == SUCCESS )
F12 return SUCCESS;
printMap( row, column );
F13 if ( move( row - 1, column )
F14 == SUCCESS )
F15 return SUCCESS;
printMap( row, column );
F16 if ( move( row, column - 1 )
F17 == SUCCESS )
F18 return SUCCESS;
printMap( row, column );
}
F19 return 0;
Fe }
// 프로그램 시작
1 public static void main( String[] args ) {
Scanner scan = new Scanner( System.in );
// 캐릭터 현재 위치를 입력받기
System.out.print( "캐릭터 현재 위치의 줄 번호를 입력하세요: " );
int row = scan.nextInt();
System.out.print( "캐릭터 현재 위치의 칸 번호를 입력하세요: " );
int column = scan.nextInt();
// 미로 맵에서 길 찾기
2 if ( move( row, column )
3 == SUCCESS )
4 System.out.println( "\n미로 맵 탈출 성공!" );
5 else
6 System.out.println( "\n미로 맵 탈출 실패" );
scan.close();
// 프로그램 종료
7 }
}