본문 바로가기
알고리즘 문제풀이/백준

[백준 16954] 움직이는 미로 탈출(JAVA)

by 소보루:-) 2020. 3. 27.

https://www.acmicpc.net/problem/16954

 

16954번: 움직이는 미로 탈출

욱제는 학교 숙제로 크기가 8×8인 체스판에서 탈출하는 게임을 만들었다. 체스판의 모든 칸은 빈 칸 또는 벽 중 하나이다. 욱제의 캐릭터는 가장 왼쪽 아랫 칸에 있고, 이 캐릭터는 가장 오른쪽 윗 칸으로 이동해야 한다. 이 게임의 특징은 벽이 움직인다는 점이다. 1초마다 모든 벽이 아래에 있는 행으로 한 칸씩 내려가고, 가장 아래에 있어서 아래에 행이 없다면 벽이 사라지게 된다. 욱제의 캐릭터는 1초에 인접한 한 칸 또는 대각선 방향으로 인접한 한 칸으로

www.acmicpc.net

 

 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;

class Pos {
	int t;
	int x;
	int y;

	public Pos(int t,int x, int y) {
		super();
		this.t = t;
		this.x = x;
		this.y = y;
	}
}

public class Main_bj_16954_움직이는미로탈출{

	static int ans = 0;
	static char[][] map = new char[8][8];
	static boolean[][][] visit = new boolean[9][8][8];
	static int[] dx = { 0, 0, 1, -1, 1, 1, -1, -1, 0 };
	static int[] dy = { 1, -1, 0, 0, -1, 1, -1, 1, 0 };

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		for (int i = 0; i < 8; i++) {
			map[i] = br.readLine().toCharArray();
		}
		Queue<Pos> q=new LinkedList<Pos>();
		q.offer(new Pos(0,7,0));
		visit[0][7][0]=true;
		
		while(!q.isEmpty()) {
			Pos p=q.poll();
			if(p.x==0 && p.y==7) {
				System.out.println(1);
				System.exit(0);
			}
			for(int i=0; i<9; i++) {
				int nx=p.x+dx[i];
				int ny=p.y+dy[i];
				int nt=Math.min(p.t+1, 8);
				if(nx<0||ny<0||nx>=8||ny>=8)
					continue;
				if(nx-p.t>=0 && map[nx-p.t][ny]=='#')continue;
				if(nx-p.t-1>=0 && map[nx-p.t-1][ny]=='#')continue;
				
				if(visit[nt][nx][ny]==false) {
					visit[nt][nx][ny]=true;
					q.offer(new Pos(nt,nx,ny));
				}
			}
		}
		System.out.println(0);
	}
	
}

댓글