https://velog.io/@uhan2/%EC%9D%B4%EA%B2%83%EC%9D%B4-%EC%BD%94%EB%94%A9-%ED%85%8C%EC%8A%A4%ED%8A%B8%EB%8B%A4.Part02.Chapter04-%EA%B2%8C%EC%9E%84-%EA%B0%9C%EB%B0%9C-Java

 

[이것이 코딩 테스트다.Part02.Chapter04] - 게임 개발 (Java)

todayilearned.Algorithm.solveProblem(게임 개발)

velog.io

 

118~121pg

 

[이것이 코딩테스트다] 미로 탈출 - BFS 관련 문제 같은경우

BFS, DFS를 사용해야했기에 큐, 스택을 이용하기 위해 따로 Node구현체를 만들었습니다.

 

하지만 이번문제 같은경우, DFS나 BFS같은 탐색을 하는 것이 아니기에 필요하지 않습니다.

 

필요한것은

1.해당 맵을 방문했는지 확인하는 visited[50][50] 배열

2.전체 맵배열 arr[50][50]

 

public class Main {
	
	public static int n,m,x,y,direction;
//	방문한 위치를 저장하기 위한 맵을 생성하여 0으로 초기화
	public static int[][] visited = new int[50][50];
//	전체맵정보
	public static int[][] arr = new int[50][50];
	
//	북,동,남,서 방향 정의
	public static int dx[] = {-1, 0, 1, 0};
	public static int dy[] = {0, 1, 0, -1};
	
//	왼쪽으로 회전
	public static void turn_left() {
		direction -= 1;
		if( direction == -1) direction = 3;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
//		N,M을 공백을 기준으로 구분하여 입력받기
		n = sc.nextInt();
		m = sc.nextInt();
		
//		현재 캐릭터의 x좌표, y좌표, 방향을 입력받기
		x = sc.nextInt();
		y = sc.nextInt();
		direction = sc.nextInt();
		visited[x][y] = 1;
		
//		전체 맵정보를 입력받기
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				arr[i][j] = sc.nextInt();
			}
		}
		
		
//		시뮬레이션시작
		int cnt = 1;
		int turn_time = 0;
		while(true) {
			//왼쪽으로 회전
			turn_left();
			int nx = x + dx[direction];
			int ny = y + dy[direction];
			//회전한 이후 정면에 가보지 않은 칸이 존재하는 경우 이동
			if(visited[nx][ny] == 0 && arr[nx][ny] == 0) {
				visited[nx][ny] = 1;
				x = nx;
				y = ny;
				cnt += 1;
				turn_time = 0;
				continue;
			}
			//회전한 이후 정면에 가보지 않은 칸이 없거나 바다인 경우
			else turn_time += 1;
			//네 방향 모두 갈 수 없는 경우
			if(turn_time == 4) {
				nx = x - dx[direction];
				ny = y - dy[direction];
				//뒤로 갈 수 있다면 이동하기
				if(arr[nx][ny] == 0) {
					x = nx;
					y = ny;
				}
				//뒤가 바다로 막혀있는 경우
				else break;
				turn_time = 0;
			}
		}
		
		System.out.println(cnt);
	}
	
}

 

+ Recent posts