๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ

[BOJ] 3190 ๋ฑ€ - Java

category Algorithm/BOJ 2023. 9. 14. 00:18

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

 

3190๋ฒˆ: ๋ฑ€

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

www.acmicpc.net


1. ๋ฌธ์ œ

NxN ํฌ๊ธฐ์˜ ๋งต ์œ„์—์„œ ๋ฑ€์ด ์ด๋™ํ•œ๋‹ค.

์ฒ˜์Œ์— ๋ฑ€์˜ ๊ธธ์ด๋Š” 1, ์ขŒํ‘œ๋Š” 1, 1, ๋ฐฉํ–ฅ์€ ์˜ค๋ฅธ์ชฝ์„ ํ–ฅํ•œ๋‹ค.

๋ฑ€์€ ๋งค ์ดˆ๋งˆ๋‹ค ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ทœ์น™์„ ๋”ฐ๋ฅธ๋‹ค.

1. ๋จธ๋ฆฌ๋ฅผ ๋‹ค์Œ์นธ์— ์œ„์น˜์‹œํ‚จ๋‹ค.

2. ์œ„์น˜๊ฐ€ ๋ฒฝ์ด๊ฑฐ๋‚˜ ๋ชธ์ผ ๊ฒฝ์šฐ ์ข…๋ฃŒ๋œ๋‹ค.

3. ๋จธ๋ฆฌ๊ฐ€ ์œ„์น˜ํ•œ ์นธ์— ์‚ฌ๊ณผ๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ผฌ๋ฆฌ๋Š” ์›€์ง์ด์ง€ ์•Š๊ณ ,
    ์‚ฌ๊ณผ๊ฐ€ ์—†๋‹ค๋ฉด ๊ผฌ๋ฆฌ๊ฐ€ ์žˆ๋Š” ์นธ์„ ๋น„์›Œ์ค€๋‹ค. ์ฆ‰, ๋ชธ ๊ธธ์ด๋Š” ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

๊ฒŒ์ž„์ด ๋ช‡ ์ดˆ๋งŒ์— ๋๋‚˜๋Š”์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋ผ.

 

2. ํ’€์ด

๊ณจ๋“œ4

์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ์ด๋‹ค.

๊ฐœ์ธ์ ์œผ๋กœ ์ข‹์•„ํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ธ ํ๋ฅผ 2๊ฐœ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

๋ชธ์˜ ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•  bodyQ, ๋ณ€ํ™”ํ•  ์‹œ๊ฐ„๊ณผ ๋ฐฉํ–ฅ์„ ์ €์žฅํ•  dirQ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

๋ฐฉํ–ฅ ์ „ํ™˜์€ dy, dx๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

๊ทธ๋ฆฌ๊ณ  map ๊ฐ’์ด ๋นˆ ์นธ์ผ ๋•Œ, ์‚ฌ๊ณผ ์ผ ๋•Œ, ๋ชธ์ผ ๋•Œ๋ฅผ ๊ตฌ๋ถ„ํ•  ๋•Œ ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด static final๋กœ ์„ ์–ธํ•˜์—ฌ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

 

y, x ๋‘˜ ๋‹ค ์‹œ์ž‘์€ 1, ๋ฐฉํ–ฅ์€ 0์œผ๋กœ ์‹œ์ž‘ํ•˜์˜€๋‹ค.

map์— ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ณ , bodyQ์— ์‹œ์ž‘์ง€์  ์ขŒํ‘œ๋ฅผ ๋„ฃ์–ด์ค€ ํ›„ ์ถœ๋ฐœํ•œ๋‹ค.

 

1. ์ด๋™

 while๋ฌธ์ด ๋Œ ๋•Œ๋งˆ๋‹ค sec๋Š” 1์”ฉ ๋Š˜์–ด๋‚œ๋‹ค.

1) ๋จผ์ € ์ด๋™ํ•  ์ขŒํ‘œ์˜ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.

     - ๋ฒฝ์ด๋ฉด ์ข…๋ฃŒ (๋ฒ”์œ„๋ฅผ ๋„˜์–ด๊ฐˆ ๊ฒฝ์šฐ)

     - ๋ชธ์ด ์žˆ์œผ๋ฉด ์ข…๋ฃŒ

2) ๊ผฌ๋ฆฌ ์‚ญ์ œ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•œ๋‹ค.

     - ์‚ฌ๊ณผ๊ฐ€ ์žˆ์œผ๋ฉด ๊ทธ๋Œ€๋กœ ๋‘”๋‹ค.

     - ๋นˆ ์นธ์ด๋ฉด ๊ผฌ๋ฆฌ๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

์ด๋™ํ•  ๋•Œ๋งˆ๋‹ค ์‹ ๊ฒฝ์จ์•ผ ํ•˜๋Š” ๊ฒƒ์€

bodyQ์™€ map ๊ฐ’์ด๋‹ค.

 

2. ๋ฐฉํ–ฅ ์„ค์ •

์ด๋™์ด ๋๋‚˜๋ฉด setDir()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฐฉํ–ฅ์„ ์„ค์ •ํ•ด์ค€๋‹ค.

ํ˜„์žฌ ์‹œ๊ฐ„์„ ์ธก์ •ํ•˜๊ณ  dirQ์˜ ์‹œ๊ฐ„๊ณผ ์ผ์น˜ํ•  ๊ฒฝ์šฐ (์˜ค๋ฆ„์ฐจ์ˆœ ์ž…๋ ฅ์ด ๋ณด์žฅ๋จ)

dirQ์˜ ๋ฐฉํ–ฅ์— ๋งž๊ฒŒ ํ˜„์žฌ ๋ฐฉํ–ฅ์ด ๊ฐฑ์‹ ๋œ๋‹ค.

D์ผ ๊ฒฝ์šฐ ์˜ค๋ฅธ์ชฝ์œผ๋กœ 90๋„, L์ผ ๊ฒฝ์šฐ ์™ผ์ชฝ์œผ๋กœ 90๋„์ด๋‹ค.

 

์ด๋™, ๋ฐฉํ–ฅ์„ค์ •์ด ๋ชจ๋‘ ๋๋‚ฌ์œผ๋‹ˆ ๊ฒฐ๊ตญ์—๋Š” ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์— ์˜ํ•ด ์ด๋™์ด ๋๋‚˜๊ฒŒ ๋˜๊ณ , ๊ทธ ๋•Œ์˜ sec๋ฅผ ์ถœ๋ ฅํ•ด์ค€๋‹ค.

 

3. ์ฝ”๋“œ

import java.io.*;
import java.util.*;

public class Main {

    static int N, K, L, sec, y, x, d;
    static int[][] map;
    static Queue<Loc> bodyQ = new ArrayDeque<> ();
    static Queue<Dir> dirQ = new ArrayDeque<> ();

    static int[] dy = {0,1,0,-1};
    static int[] dx = {1,0,-1,0};

    static final int BLANK = 0;
    static final int APPLE = 1;
    static final int BODY = 2;

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = null;

        N = Integer.parseInt(br.readLine());
        map = new int[N+1][N+1];
        K = Integer.parseInt(br.readLine());
        for (int i=0; i<K; i++) {
            st = new StringTokenizer(br.readLine());
            int appleY = Integer.parseInt(st.nextToken());
            int appleX = Integer.parseInt(st.nextToken());
            map[appleY][appleX] = APPLE;
        }
        L = Integer.parseInt(br.readLine());
        for (int i=0; i<L; i++) {
            st = new StringTokenizer(br.readLine());
            int second = Integer.parseInt(st.nextToken());
            String direction = st.nextToken();
            dirQ.offer(new Dir(second, direction));
        }
        // ์ž…๋ ฅ ๋

        y = 1;
        x = 1;
        d = 0;
        map[y][x] = BODY;
        bodyQ.offer(new Loc(y, x));

        move();

        System.out.println(sec);
    }

    static void move() {
        while (true) {
            sec++;
            int ny = y+dy[d];
            int nx = x+dx[d];
            if (ny<=0 || ny>N || nx<=0 || nx>N) break;
            if (map[ny][nx] == BODY) break;
            if (map[ny][nx] == BLANK && !bodyQ.isEmpty()) {
                Loc tail = bodyQ.poll();
                map[tail.y][tail.x] = BLANK;
            }
            y = ny;
            x = nx;
            map[y][x] = BODY;
            bodyQ.offer(new Loc(y, x));

            // ๋ฐฉํ–ฅ์„ค์ •
            setDir();
        }
    }

    static void setDir() {
        if (dirQ.isEmpty()) return;
        if (sec == dirQ.peek().second) {
            Dir dir = dirQ.poll();
            if ("D".equals(dir.direction)) {
                d = (d+1)%4;
            }
            else if ("L".equals(dir.direction)) {
                d--;
                if (d<0) d = 3;
            }
        }
    }

    static class Loc {
        int y, x;
        Loc(int y, int x) {
            this.y = y;
            this.x = x;
        }
    }

    static class Dir {
        int second;
        String direction;
        Dir (int second, String direction) {
            this.second = second;
            this.direction = direction;
        }
    }
}

 

4. ์‚ฌ๋‹ด

์‹œ๊ฐ„๊ฐ€๋Š”์ค„ ๋ชจ๋ฅด๊ณ  ์žฌ๋ฐŒ๊ฒŒ ํ’€์—ˆ๋‹ค.

์‹ธํ”ผํ•  ๋•Œ๋Š” ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ, ํด๋ฆฐ์ฝ”๋“œ๋Š” ๋’ท์ „์ด๊ณ  ์–ด๋–ป๊ฒŒ๋“  ๋‹ต๋งŒ ๋‚˜์˜ค๊ฒŒ ํ•˜๋ฉด ๋œ๋‹ค! ์˜€์œผ๋‚˜

ํ˜„์žฌ๋Š” ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ์Šค์Šค๋กœ ๊ฐ€๋…์„ฑ์„ ์‹ ๊ฒฝ์“ฐ๋ฉด์„œ ์งœ๊ณ  ์žˆ๋‹ค๊ณ  ๋Š๊ผˆ๋‹ค. (๊ทธ๋ ‡๋‹ค๊ณ  ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์ง„๊ฑด ์•„๋‹˜)

 

๋ฌผ๋ก  ํšจ์œจ์ ์ด์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ํ™•์‹คํžˆ ์ด์ „๋ณด๋‹ค๋Š” ๊ฐ€๋…์„ฑ์ด ๋‚˜์•„์ ธ์„œ ๊ทธ๋Ÿฐ์ง€

๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ๋‚ด๊ฐ€ ํ—ท๊ฐˆ๋ ค์„œ ๋‹ค์‹œ ์ฝ”๋“œ๋ฅผ ์™”๋‹ค๊ฐ”๋‹ค ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์ค„์–ด๋“  ๊ฒƒ ๊ฐ™๋‹ค.

๋‹ค๋ฅธ ๋ฌธ์ œ๋“ค์„ ๋” ๋งŽ์ด ํ’€์–ด๋ด์•ผ ์ •๋ง๋กœ ๊ทธ๋Ÿฐ์ง€ ์ƒ๊ฐํ•ด๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.