알고리즘(백준 등) 공부/백준(자바)

백준 1347번: 미로 만들기

posite 2026. 4. 1. 14:32

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

 

주어진 방향 전환과 움직임을 바탕으로 미로의 지도를 그리는 문제이다. 입력에 갈 수 있는 모든 곳에 대한 정보가 담겨 있으며 길이가 최대 50이므로 행, 열의 길이가 각각 101인 배열을 원점(50, 50)을 중심으로 이동하면서 갈 수 있는 곳은 .으로 마킹하여 그리는 방식으로 풀이하였다.

 

방향은 남쪽부터 시작하여 왼쪽, 오른쪽으로 회전할 수 있으며, 2차원 배열을 이용하여 오른쪽은 +, 왼쪽은 -로 구성하였다.

int[][] directions = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
int direction = 0;

 

 

이후, 행, 열의 길이가 각각 101인 배열을 선언 및 #(벽)으로 채운 후, 시작점을 .으로 마킹한다.

char[][] board = new char[101][101];
for (int i = 0; i < 101; i++) {
    for (int j = 0; j < 101; j++) {
        board[i][j] = '#';
    }
}
int currentR = 50, currentC = 50;
board[50][50] = '.';

 

 

회전 및 이동하면서 갈 수 있는 곳에 .을 마킹하고 전진할 경우, 현재 방향에 맞게 현재 위치를 이동하며, 행과 열의 최댓값, 최솟값을 업데이트해준다.

int maxR = currentR, minR = currentR, maxC = currentC, minC = currentC;
for (int i = 0; i < n; i++) {
    if (line.charAt(i) == 'F') {
        currentR += directions[direction][0];
        currentC += directions[direction][1];
        maxR = Math.max(currentR, maxR);
        minR = Math.min(currentR, minR);
        maxC = Math.max(currentC, maxC);
        minC = Math.min(currentC, minC);
        board[currentR][currentC] = '.';
    } else if (line.charAt(i) == 'R') {
        direction++;
        direction %= 4;
    } else {
        direction--;
        if (direction == -1) {
            direction = 3;
        }
    }
}

 

 

이후 결과를 행과 열의 각각의 최댓값, 최솟값 범위에 맞추어 출력하면 된다.

StringBuilder sb = new StringBuilder();
for (int i = minR; i <= maxR; i++) {
    for (int j = minC; j <= maxC; j++) {
        sb.append(board[i][j]);
    }
    sb.append('\n');
}
System.out.print(sb);

 

 

결과 코드는 다음과 같다.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class 미로만들기1347 {
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        String line = br.readLine();
        br.close();
        
        int[][] directions = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
        int direction = 0;
        char[][] board = new char[101][101];
        for (int i = 0; i < 101; i++) {
            for (int j = 0; j < 101; j++) {
                board[i][j] = '#';
            }
        }
        int currentR = 50, currentC = 50;
        board[50][50] = '.';
        int maxR = currentR, minR = currentR, maxC = currentC, minC = currentC;
        for (int i = 0; i < n; i++) {
            if (line.charAt(i) == 'F') {
                currentR += directions[direction][0];
                currentC += directions[direction][1];
                maxR = Math.max(currentR, maxR);
                minR = Math.min(currentR, minR);
                maxC = Math.max(currentC, maxC);
                minC = Math.min(currentC, minC);
                board[currentR][currentC] = '.';
            } else if (line.charAt(i) == 'R') {
                direction++;
                direction %= 4;
            } else {
                direction--;
                if (direction == -1) {
                    direction = 3;
                }
            }
        }
        
        StringBuilder sb = new StringBuilder();
        for (int i = minR; i <= maxR; i++) {
            for (int j = minC; j <= maxC; j++) {
                sb.append(board[i][j]);
            }
            sb.append('\n');
        }
        System.out.print(sb);
    }
}