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

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ

10875. ๋ฑ€

Problem ๐Ÿ’ป

์ด ๋ฌธ์ œ๋Š” L ≤ 10^8 ์ด๋ฏ€๋กœ 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ํ•˜๋‚˜ํ•˜๋‚˜ ๋ฑ€์„ ์ด๋™์‹œ์ผœ๊ฐ€๋ฉฐ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋Š” ์•„๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋จธ๋ฆฌ์˜ ํšŒ์ „ ํšŸ์ˆ˜ N์„ ์ด์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด์•ผ ํ•œ๋‹ค.

 

 

Approach 1 โญ•

  • ๊ตฌํ˜„

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๋ฑ€์ด ๋จธ๋ฆฌ๋ฅผ 1์ดˆ๋งˆ๋‹ค ํ•˜๋‚˜์”ฉ ๋Š˜๋ ค๊ฐ€๋ฉฐ ์–ด๋–ค ์‹œ๊ฐ„์—๋Š” ํšŒ์ „๋„ ํ•˜๊ณ  ํ•˜๋ฉด์„œ ์ตœ์ข… ์ฃฝ๋Š” ์‹œ๊ฐ„์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋ฌธ์ œ์˜ ์ฃผ์ฒด๋ฅผ ๋ฑ€์ด ์•„๋‹ˆ๋ผ ์‚ฌ๋žŒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์ž์‹ ์ด ๊ฐ€๋˜ ๊ฒฝ๋กœ์™€ ๊ฒน์น˜๋Š” ์ˆœ๊ฐ„ ํ˜น์€ ๋ฒ”์œ„์˜ ๋ฐ–์œผ๋กœ ๊ฐ€๋Š” ์ˆœ๊ฐ„ ์ฃฝ๋Š” ๊ฒƒ์œผ๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋ฑ€์€ ์˜ค๋กœ์ง€ ์ˆ˜์ง, ์ˆ˜ํ‰์œผ๋กœ๋งŒ ๋จธ๋ฆฌ๊ฐ€ ๋Š˜์–ด๋‚œ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฐฉํ–ฅ์„ ๋ฐ”๊พธ๋Š” ์‹œ๊ฐ„ ์ฐจ์ด์™€ ๋ฐ”๊พธ๋Š” ๋ฐฉํ–ฅ์ด ์ฃผ์–ด์ง€๋ฉด ๋ฐ”๊พธ๋Š” ์‹œ๊ฐ„ ์ „๊นŒ์ง€๋Š” ๊ฐ™์€ ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ์ด๋™ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ํ•ด๋‹น ๋ฐฉํ–ฅ ์ „๊นŒ์ง€ ํ•˜๋‚˜ํ•˜๋‚˜ ์ด๋™ํ•˜์ง€ ์•Š๊ณ  ๋‹จ๋ฒˆ์— ‘x + time’ ๋˜๋Š” ‘y + time’์œผ๋กœ ์ขŒํ‘œ๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

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

 

ํ•˜์ง€๋งŒ, ์–ด๋ ค์šด ๊ฒƒ.. ๋ชป ํ’€์—ˆ๋‹ค ใ…œ

 

์—ฌ๋Ÿฌ ํ’€์ด ๋ฐฉ๋ฒ•์ด ์žˆ๊ฒ ์ง€๋งŒ, ๋‚ด๊ฐ€ ๋ณธ ํ’€์ด ๋ฐฉ๋ฒ•์€ ์ˆ˜์ง, ์ˆ˜ํ‰์ผ ๋•Œ ์ƒํ•˜์ขŒ์šฐ๋กœ ๋‹ค๊ฐ€๊ฐ€๋Š” ๊ฒฝ๋กœ์˜ ์‹œ์ž‘ ์ ๊ณผ ๋ณธ๋ž˜ ๊ฒฝ๋กœ์˜ ์‹œ์ž‘ ์  ์‚ฌ์ด์˜ ์ฐจ์ด ์ค‘ ์ž‘์€ ๊ฐ’์„ ๊ณ ๋ฅด๊ณ  ํ•ด๋‹น ๊ฐ’์ด ๋ฐฉํ–ฅ์„ ํšŒ์ „ํ•˜๋Š” ์‹œ๊ฐ„์˜ ์ฐจ์ด๋ณด๋‹ค ํฌ๋ฉด ๋ถ€๋”ชํžŒ ๊ฒƒ์ด๊ณ  ์ž‘์œผ๋ฉด ๋ถ€๋”ชํžˆ์ง€ ์•Š์€ ๊ฒƒ!

 

 

์ด์ „์˜ ์ˆ˜ํ‰ ๊ฒฝ๋กœ๋ฅผ ์•„๋ž˜์—์„œ ์œ„๋กœ ๋ถ€๋”ชํžˆ๋Š” ๊ฒฝ์šฐ(์ƒํ•˜์ขŒ์šฐ ์ค‘ ์ƒ)๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ๋ณด๋ฉด ์œ„์™€ ๊ฐ™๋‹ค. time ์ฆ‰, ๋ฐฉํ–ฅ์„ ๋ฐ”๊พธ๊ธฐ ์ „ ํ˜„์žฌ ์›€์ง์ด๋˜ ๊ฒฝ๋กœ๋ฅผ ๋ชจ๋‘ ์›€์ง์ธ ๊ฒฝ์šฐ์˜ ๊ธธ์ด์™€ ํ˜„์žฌ ๊ฒฝ๋กœ์˜ ์‹œ์ž‘์ ๊ณผ ์ˆ˜ํ‰ ๊ฒฝ๋กœ์˜ ์ฐจ์ด ์ค‘ ํ•ด๋‹น ๊ฒฝ๋กœ๋ฅผ ๋ชจ๋‘ ์›€์ง์ธ ๊ฒฝ์šฐ๊ฐ€ ๋” ํฌ๋‹ค๋ฉด, ๋ฌด์กฐ๊ฑด ๋ถ€๋”ชํžˆ๊ฒŒ ๋˜์–ด ์žˆ๊ณ  ์ž‘์œผ๋ฉด ๋ถ€๋”ชํžˆ์ง€ ์•Š๋Š”๋‹ค!

 

์ด๊ฑธ 8๊ฐ€์ง€ ๊ฒฝ์šฐ์— ๋ชจ๋‘ ์ƒ๊ฐํ•ด์„œ ์ ์šฉํ•ด์ฃผ๋ฉด ์ •๋‹ต์ด ๋œ๋‹ค.

 

Solution ๐Ÿ’ก

public class BOJ_10875 {

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

    private static class Command {
        long time;
        char cmd;

        public Command(long time, char cmd) {
            this.time = time;
            this.cmd = cmd;
        }
    }

    private static class Snake {
        long sy, sx;
        long ey, ex;

        public Snake(long sy, long sx, long ey, long ex) {
            this.sy = sy;
            this.sx = sx;
            this.ey = ey;
            this.ex = ex;

            if (this.sy > this.ey) {
                long tmp = this.sy;
                this.sy = this.ey;
                this.ey = tmp;
            }
            if (this.sx > this.ex) {
                long tmp = this.sx;
                this.sx = this.ex;
                this.ex = tmp;
            }
        }
    }

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

        int l = Integer.parseInt(br.readLine());
        int n = Integer.parseInt(br.readLine());
        ArrayList<Command> cmd = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            long time = Long.parseLong(st.nextToken());
            char c = st.nextToken().charAt(0);

            cmd.add(new Command(time, c));
        }

        ArrayList<Snake> snakes = new ArrayList<>();
        snakes.add(new Snake(-l-1, -l-1, l+1, -l-1));
        snakes.add(new Snake(l+1, -l-1, l+1, l+1));
        snakes.add(new Snake(l+1, l+1, -l-1, l+1));
        snakes.add(new Snake(-l-1, l+1, -l-1, -l-1));
        long y = 0;
        long x = 0;
        int dir = 0;

        long ret = 0;

        boolean done = false;
        Command command;
        for (int i = 0; i < cmd.size()+1; i++) {
            if (i == cmd.size()) {
                command = new Command(Long.MAX_VALUE, 'L');
            } else {
                command = cmd.get(i);
            }

            long t = Long.MAX_VALUE;
            for (int idx = 0; idx < snakes.size(); idx++) {
                if (snakes.get(idx).ey == snakes.get(idx).sy) {
                    if (dir == 0) { // ์˜ค๋ฅธ
                        if (y == snakes.get(idx).ey && x < snakes.get(idx).sx)
                            t = Math.min(t, snakes.get(idx).sx - x);
                    } else if (dir == 1) { // ์œ„
                        if (snakes.get(idx).sx <= x && x <= snakes.get(idx).ex && y < snakes.get(idx).sy)
                            t = Math.min(t, snakes.get(idx).sy - y);
                    } else if (dir == 2) { // ์™ผ
                        if (snakes.get(idx).ey == y && x > snakes.get(idx).ex)
                            t = Math.min(t, x - snakes.get(idx).ex);
                    } else { // ์•„๋ž˜
                        if (snakes.get(idx).sx <= x && x <= snakes.get(idx).ex && y > snakes.get(idx).sy)
                            t = Math.min(t, y - snakes.get(idx).sy);
                    }
                } else if (snakes.get(idx).ex == snakes.get(idx).sx) {
                    if (dir == 0) {
                        if (snakes.get(idx).sy <= y && y <= snakes.get(idx).ey && x < snakes.get(idx).sx)
                            t = Math.min(t, snakes.get(idx).sx - x);
                    } else if (dir == 1) {
                        if (snakes.get(idx).sx == x && y < snakes.get(idx).sy)
                            t = Math.min(t, snakes.get(idx).sy - y);
                    } else if (dir == 2) {
                        if (snakes.get(idx).ey >= y && y >= snakes.get(idx).sy && x > snakes.get(idx).sx)
                            t = Math.min(t, x - snakes.get(idx).sx);
                    } else {
                        if (snakes.get(idx).sx == x && y > snakes.get(idx).ey)
                            t = Math.min(t, y - snakes.get(idx).ey);
                    }
                }
            }

            if (command.time < t) {
                snakes.add(new Snake(y, x, y + dy[dir] * command.time, x + dx[dir] * command.time));
                ret += command.time;
                y = y + dy[dir] * command.time;
                x = x + dx[dir] * command.time;

                if (command.cmd == 'L') dir = (dir + 1) % 4;
                else dir = (dir - 1 < 0) ? 3 : dir - 1;
            } else {
                ret += t;
                break;
            }
        }

        System.out.println(ret);
    }
}