문제를 보자마자 BFS 라는 것은 파악했으나
미로에서 이동하지 못하는 벽이 움직인다는 조건 때문에 많이 헤맸다.
캐릭터가 움직인 후, 벽이 움직인다
BFS로 다음으로 이동할 경로를 탐색한 후에 벽을 이동하는 함수를 호출해야 하는데 어느 지점에서 호출해야 할지 감이 오지 않았다.
출발지(왼쪽 최하단): (7, 0)
목적지(오른쪽 최상단): (0, 7)
#이 아닌 위치현재 레벨의 노드들을 모두 탐색
int size = q.size();
for (int s = 0; s < size; s++) {
Node tmp = q.poll();
// 노드 탐색 작업
}
다음 레벨의 노드들을 큐에 추가
for (int i = 0; i < 9; i++) {
int newX = x + dx[i];
int newY = y + dy[i];
if (isValid(newX, newY) && map[newX][newY] == '.') {
q.offer(new Node(newX, newY));
}
}
1초간의 캐릭터 이동 종료
.으로 채운다.큐에서 꺼낸 노드의 위치
Queue.poll()= 현재 캐릭터의 위치
import java.io.BufferedReader;
import java.io.IOException;
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 Node{
int x;
int y;
Node(int x, int y){
this.x = x;
this.y = y;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public void setX(int x) {
this.x = x;
}
}
public class Main {
static char[][] map = new char[8][8];
static List<Node> walls = new ArrayList<>();
static int[] dx = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
static int[] dy = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
public static boolean isValid(int x, int y) {
return x >= 0 && x < 8 && y >= 0 && y < 8;
}
public static void moveWall() {
for(int i=6;i>=0;i--){
for(int j=0;j<8;j++){
map[i+1][j] = map[i][j];
}
}
//첫번째 행은 모두 '.'으로 변경
for(int i=0;i<8;i++){
map[0][i] = '.';
}
}
public static int bfs(int x, int y) {
Queue<Node> q = new LinkedList<>();
q.offer(new Node(x, y));
while(!q.isEmpty()) {
int size = q.size();
for(int s = 0; s < size; s++) {
Node tmp = q.poll();
x = tmp.getX();
y = tmp.getY();
if(map[x][y] == '#') continue;
if(x == 0 && y == 7) return 1;
for(int i = 0; i < 9; i++) {
int newX = x + dx[i];
int newY = y + dy[i];
if(isValid(newX, newY)) {
if(map[newX][newY] == '.')
q.offer(new Node(newX, newY));
}
}
}
moveWall();
}
return 0;
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int i = 0; i < 8; i++) {
String input = br.readLine();
for(int j = 0; j < 8; j++) {
map[i][j] = input.charAt(j);
}
}
System.out.println(bfs(7, 0));
}
}
입력 받을때 벽의 위치를 저장하는 배열을 따로 선언해서
벽의 이동을 관리한다면 더 효율적인 코드가 나오지 않을까? 라는 생각이 든다.
[백준] code.plus(BFS 알고리즘,JAVA)16954번, 움직이는 미로 탈출
[백준] 16954번 - 움직이는 미로 탈출 (Java)(○)