๊ธฐ๋ก๋ฐฉ

BOJ_3190 : ๋ฑ€ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_3190 : ๋ฑ€

Soom_1n 2024. 4. 11. 19:44

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

3190๋ฒˆ: ๋ฑ€

'Dummy' ๋ผ๋Š” ๋„์Šค๊ฒŒ์ž„์ด ์žˆ๋‹ค. ์ด ๊ฒŒ์ž„์—๋Š” ๋ฑ€์ด ๋‚˜์™€์„œ ๊ธฐ์–ด๋‹ค๋‹ˆ๋Š”๋ฐ, ์‚ฌ๊ณผ๋ฅผ ๋จน์œผ๋ฉด ๋ฑ€ ๊ธธ์ด๊ฐ€ ๋Š˜์–ด๋‚œ๋‹ค. ๋ฑ€์ด ์ด๋ฆฌ์ €๋ฆฌ ๊ธฐ์–ด๋‹ค๋‹ˆ๋‹ค๊ฐ€ ๋ฒฝ ๋˜๋Š” ์ž๊ธฐ์ž์‹ ์˜ ๋ชธ๊ณผ ๋ถ€๋”ชํžˆ๋ฉด ๊ฒŒ์ž„์ด ๋๋‚œ๋‹ค. ๊ฒŒ์ž„

www.acmicpc.net


๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • NxN ๊ฒŒ์ž„ํŒ์—์„œ ๋ฑ€์ด ์ด๋™ํ•˜๋Š” ๊ฒŒ์ž„์ด ๋ช‡์ดˆ๊ฐ„ ์ง„ํ–‰ ๋˜๋Š”์ง€ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ๊ฒŒ์ž„ ํŒ์—๋Š” K๊ฐœ์˜ ์‚ฌ๊ณผ๊ฐ€ ์žˆ๊ณ , ์‚ฌ๊ณผ๋ฅผ ๋จน์œผ๋ฉด ๋ชธ ๊ธธ์ด๊ฐ€ 1 ๋Š˜์–ด๋‚˜๋ฉฐ ๊ธฐ์กด์˜ ๊ผฌ๋ฆฌ ์œ„์น˜๊ฐ€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค.
  • ๋ชธ ํ˜น์€ ๊ฒŒ์ž„ํŒ ๋ฒฝ์— ๋ถ€๋”ชํžˆ๋ฉด ๊ฒŒ์ž„์ด ์ข…๋ฃŒ๋œ๋‹ค.

๐Ÿ”ธ ๋ฌธ์ œ ํ’€์ด ๐Ÿ”ธ

  • ํŠน๋ณ„ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์—†๋Š” ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ์ด๋‹ค.
  • ๋ฑ€์˜ ๋ฐฉํ–ฅ ์ „ํ™˜ ์ •๋ณด์™€ ๋ฑ€์˜ ๊ผฌ๋ฆฌ ์ขŒํ‘œ๋ฅผ ๊ธฐ์–ตํ•˜๊ธฐ ์œ„ํ•ด ํ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.*;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        // Input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = null;

        byte N = Byte.parseByte(br.readLine());
        byte[][] board = new byte[N + 1][N + 1]; // ๊ฒŒ์ž„ํŒ

        byte K = Byte.parseByte(br.readLine());

        for (int i = 0; i < K; i++) { // ์‚ฌ๊ณผ ์œ„์น˜ ๊ธฐ๋ก : 1
            st = new StringTokenizer(br.readLine());
            board[Byte.parseByte(st.nextToken())][Byte.parseByte(st.nextToken())] = 1;
        }

        byte L = Byte.parseByte(br.readLine());
        Queue<Turn> que = new ArrayDeque<>(); // ํšŒ์ „ ์ •๋ณด ์ €์žฅ

        for (int i = 0; i < L; i++) { // ํšŒ์ „ ์ •๋ณด ๊ธฐ๋ก
            st = new StringTokenizer(br.readLine());
            que.add(new Turn(Short.parseShort(st.nextToken()), st.nextToken().charAt(0) == 'L' ? (byte) 3 : (byte) 1));
        }

        // Play
        int answer = playDummy(board, que);

        // Output
        bw.write(Integer.toString(answer));
        bw.flush();
    }

    private static int playDummy(byte[][] board, Queue<Turn> que) {
        // ๋ฐฉํ–ฅ ์ •๋ณด ์ดˆ๊ธฐํ™”
        int[] dx = {-1, 0, 1, 0};
        int[] dy = {0, 1, 0, -1};
        int direction = 1; // ๋ฑ€์€ ์˜ค๋ฅธ์ชฝ์„ ๋ณด๊ณ  ์‹œ์ž‘

        // ์ขŒํ‘œ ์ •๋ณด ์ดˆ๊ธฐํ™”
        Position now = new Position(1, 1);
        board[1][1] = 2;

        // ๊ผฌ๋ฆฌ ์ •๋ณด
        Queue<Position> tail = new ArrayDeque<>();
        tail.add(new Position(1, 1));

        Turn turn = que.poll();

        int time = 0;

        while (true) {
            // ์‹œ๊ฐ„ ์ฆ๊ฐ€
            time++;


            // ์ด๋™
            now.row += dx[direction];
            now.col += dy[direction];

            // ๋ฐฉํ–ฅ ๋ณ€๊ฒฝ
            if (time == turn.X) {
                direction = (direction + turn.C) % 4;
                if (!que.isEmpty()) {
                    turn = que.poll();
                }
            }

            // ๊ฒŒ์ž„ํŒ ๋ฒ—์–ด๋‚˜๋ฉด ์ข…๋ฃŒ
            if (now.row <= 0 || board.length <= now.row || now.col <= 0 || board[0].length <= now.col) {
                return time;
            }

            if (board[now.row][now.col] == 0) { // ์ผ๋ฐ˜ ์ด๋™
                board[now.row][now.col] = 2;
                if (!tail.isEmpty()) { // ๊ผฌ๋ฆฌ ์—†์•ฐ
                    Position tp = tail.poll();
                    board[tp.row][tp.col] = 0;
                }
                tail.add(new Position(now.row, now.col));
            } else if (board[now.row][now.col] == 1) { // ์‚ฌ๊ณผ ๋จน์Œ
                board[now.row][now.col] = 2;
                tail.add(new Position(now.row, now.col));
            } else { // ๋ฑ€ ๋ชธ์— ๋ถ€๋”ชํž˜
                return time;
            }
        }
    }

    private static class Position {
        int row, col;

        public Position(int row, int col) {
            this.row = row;
            this.col = col;
        }
    }

    private static class Turn {
        short X;
        Byte C;

        public Turn(short x, Byte c) {
            X = x;
            C = c;
        }
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์ฝ”๋“œ์˜ ํ๋ฆ„์€ ์ฃผ์„์œผ๋กœ ์ž˜ ์ •๋ฆฌ๋˜์–ด ์žˆ์œผ๋‹ˆ, ๊ฐ„๋‹จํ•œ ํ๋ฆ„๋งŒ ์„ค๋ช…ํ•œ๋‹ค.
    • ๊ฒŒ์ž„ํŒ์— ์‚ฌ๊ณผ ์œ„์น˜ ์ •๋ณด๋ฅผ ๊ธฐ๋กํ•œ๋‹ค.
    • ๋ฐฉํ–ฅ ์ „ํ™˜ ์ •๋ณด๋ฅผ ํ์— ๋„ฃ๋Š”๋‹ค.
    • ๊ฒŒ์ž„์„ ์ง„ํ–‰ํ•˜๋ฉฐ ๊ผฌ๋ฆฌ ์ขŒํ‘œ๋ฅผ ํ์— ๋„ฃ๋Š”๋‹ค.
    • ๊ฒŒ์ž„ํŒ์„ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜ ๋ชธ์— ๋ถ€๋”ชํžˆ๋ฉด ๊ฒŒ์ž„์„ ์ข…๋ฃŒํ•œ๋‹ค.
  • ์ด๋™ ํ›„์— ๋ฐฉํ–ฅ์„ ๋ณ€๊ฒฝํ•˜๋Š” ์ ์„ ์œ ์˜ํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๋‹จ์ˆœ ๊ตฌํ˜„ ๋ฌธ์ œ์—ฌ์„œ ํฌ๊ฒŒ ์–ด๋ ค์šด ๋ถ€๋ถ„์€ ์—†์—ˆ๋‹ค.
    • ๋ฌธ์ œ์—์„œ ์ž์„ธํžˆ ์„ค๋ช…๋˜์–ด ์ง€์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™์€๋ฐ, ๋ฑ€์˜ ์ด๋™ ์ดํ›„์— ๋ฐฉํ–ฅ์ด ๋ณ€๊ฒฝ๋˜๋Š” ๋ถ€๋ถ„์ด ์žˆ์–ด์„œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ๋ณด๊ณ  ์กฐ๊ธˆ ๋””๋ฒ„๊น… ํ•  ํ•„์š”๊ฐ€ ์žˆ์—ˆ๋‹ค.
  • ์ž…๋ ฅ ๊ฐ’์ด 100 ์ดํ•˜์ธ ๋ถ€๋ถ„์ด ๋งŽ์•„์„œ ๊ฐ‘์ž๊ธฐ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์•„๊ปด์„œ ๊ตฌํ˜„ํ•ด ๋ณด๊ณ  ์‹ถ์—ˆ๋‹ค.
    • -128~127 ๊นŒ์ง€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ Byte์™€ X๋งŒ ๊ฐ’์ด ์ปค์„œ Short๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•ด๋ณด์•˜๋‹ค.
    • ํ•˜์ง€๋งŒ NumberFormatException์ด ๋– ์„œ ๊ทธ๋ƒฅ ์ „๋ถ€ int๋กœ ๋ฐ”๊ฟ”๋ณด๋‹ˆ ํ†ต๊ณผ ๋˜์—ˆ๋‹ค.
    • ๋ญ๊ฐ€ ๋ฌธ์ œ์ธ๊ฐ€ ๋‹ค์‹œ ์‚ดํŽด๋ณด๋‹ˆ X๋งŒ Shortํ˜•์ธ๋ฐ Byte๋กœ ์ž…๋ ฅ์„ ๋ณ€ํ™˜ํ•œ๊ฒŒ ๋ฌธ์ œ์˜€๋‹ค.
    • ์—ญ์‹œ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ์—์„œ๋Š” ์ด๋ ‡๊ฒŒ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์•„๋ผ์ง„ ์•Š์•„๋„ ๋˜๋‹ˆ int ํ˜น์€ long์„ ์“ฐ๋Š”๊ฒŒ ๋งž๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
728x90