๊ธฐ๋ก๋ฐฉ

BOJ_9019 : DSLR ๋ณธ๋ฌธ

CodingTest/Java

BOJ_9019 : DSLR

Soom_1n 2023. 3. 21. 23:06

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

 

9019๋ฒˆ: DSLR

๋„ค ๊ฐœ์˜ ๋ช…๋ น์–ด D, S, L, R ์„ ์ด์šฉํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ๊ธฐ๊ฐ€ ์žˆ๋‹ค. ์ด ๊ณ„์‚ฐ๊ธฐ์—๋Š” ๋ ˆ์ง€์Šคํ„ฐ๊ฐ€ ํ•˜๋‚˜ ์žˆ๋Š”๋ฐ, ์ด ๋ ˆ์ง€์Šคํ„ฐ์—๋Š” 0 ์ด์ƒ 10,000 ๋ฏธ๋งŒ์˜ ์‹ญ์ง„์ˆ˜๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ ๋ช…๋ น์–ด๋Š” ์ด ๋ ˆ์ง€์Šคํ„ฐ์—

www.acmicpc.net



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

  • T๋ฒˆ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ๋Œ๋ฆฐ๋‹ค.
    • ๊ณ„์‚ฐ๊ธฐ๋Š” 10์ง„์ˆ˜ 4์ž๋ฆฌ ์ˆซ์ž๋กœ 0000~9999๊นŒ์ง€ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
    • 4๊ฐ€์ง€ ์—ฐ์‚ฐ D, S, L, R์ด ์žˆ๋‹ค.
    • A๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด B๊ฐ€ ๋˜๋Š” ์ตœ์†Œ๊ธธ์ด ๋ช…๋ น์–ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • ๊ฐ€์ค‘์น˜๊ฐ€ ์—†๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ(์ตœ์†Œ๊ฒฝ๋กœ) ๋ฌธ์ œ์ด๋ฏ€๋กœ BFS ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
    • ํ์—์„œ ํ˜„์žฌ ์ˆซ์ž์™€ ์ง€๊ธˆ๊นŒ์ง€์˜ ์—ฐ์‚ฐ์„ ๊ธฐ๋กํ•  ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ ๋‹ค.
    • D : 2n%10000
    • S : n-1 (n=1์ด๋ฉด, 9999)
    • L : (LShift) n = n*10%10000 + n/1000
    • R : (RShift) n = n/10 + n%10*1000

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
    private static class DSLR {
        String history;
        int num;

        public DSLR(int num, String history) {
            this.num = num;
            this.history = history;
        }
    }

    private static final int MAX = 10_000;

    public static void main(String[] args) throws IOException {
        // Input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        StringBuilder sb = new StringBuilder();
        StringTokenizer st = null;

        for (int t = 0; t < T; t++) {
            st = new StringTokenizer(br.readLine());
            int A = Integer.parseInt(st.nextToken());
            int B = Integer.parseInt(st.nextToken());
            boolean[] visited = new boolean[MAX];

            // BFS
            Queue<DSLR> que = new ArrayDeque<>();
            que.offer(new DSLR(A, ""));

            while (!que.isEmpty()) {
                DSLR dslr = que.poll();
                int num = dslr.num;
                String history = dslr.history;

                // B๋ฐœ๊ฒฌํ•˜๋ฉด ์ข…๋ฃŒ
                if (num == B) {
                    sb.append(history).append('\n');
                    break;
                }

                if (!visited[num]) {
                    visited[num] = true;

                    // D ์—ฐ์‚ฐ
                    int temp = (num * 2) % MAX;
                    if (!visited[temp]) {
                        que.offer(new DSLR(temp, history + 'D'));
                    }

                    // S ์—ฐ์‚ฐ
                    temp = num == 0 ? 9999 : num - 1;
                    if (!visited[temp]) {
                        que.offer(new DSLR(temp, history + 'S'));
                    }

                    // L ์—ฐ์‚ฐ
                    temp = (num * 10) % 10000 + num / 1000;
                    if (!visited[temp]) {
                        que.offer(new DSLR(temp, history + 'L'));
                    }

                    // R ์—ฐ์‚ฐ
                    temp = num / 10 + num % 10 * 1000;
                    if (!visited[temp]) {
                        que.offer(new DSLR(temp, history + 'R'));
                    }
                }
            }
        }
        // Output
        System.out.println(sb);
    }
}

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

  • ์ค‘๋ณต ์—ฐ์‚ฐ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐฉ๋ฌธ์ฒดํฌ ์šฉ boolean ๋ฐฐ์—ด visited๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • DSLR ํด๋ž˜์Šค๋Š” ํ˜„์žฌ ์ˆซ์ž num๊ณผ ์ง€๊ธˆ ๊นŒ์ง€์˜ ์—ฐ์‚ฐ ๊ธฐ๋ก์ธ history๋ฅผ ๊ฐ–๊ณ ์žˆ๋‹ค.
  • ํ์—์„œ pollํ•œ ๋’ค B์ด๋ฉด history๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  while๋ฌธ์„ ์ข…๋ฃŒํ•œ๋‹ค.
  • B๊ฐ€ ์•„๋‹ˆ๊ณ , ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ˆซ์ž๋ผ๋ฉด 4๊ฐ€์ง€ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ด ํ์— ๋‹ด๋Š”๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • sift๋ฅผ ํ๋กœ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ์ž˜๋ชป ์ƒ๊ฐํ•ด์„œ DSLRํด๋ž˜์Šค์˜ num์„ deque๋กœ ๊ตฌํ˜„ํ–ˆ๋”๋‹ˆ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค.
  • visited๋ฅผ true๋กœ ๋ฐ”๊พธ๋Š” ์ง€์ ์„ que.addํ•˜๋Š” ๊ณณ์œผ๋กœ ์ž˜๋ชป ์ƒ๊ฐํ–ˆ๋‹ค๊ฐ€, que.poll์œผ๋กœ ์ด๋™์‹œ์ผฐ๋‹ค.
  • ๋ฌธ์ œ์˜ ๋ถ„์„๊ณผ ํ’€์ด ๊ณ„ํš์„ ์ ์–ด๊ฐ€๋ฉฐ ํ’€์ดํ–ˆ๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_7662 : ์ด์ค‘ ์šฐ์„ ์ˆœ์œ„ ํ  (0) 2023.03.29
BOJ_5430 : AC  (0) 2023.03.28
BOJ_14502 : ์—ฐ๊ตฌ์†Œ  (0) 2023.03.08
BOJ_1916 : ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ  (0) 2023.03.08
BOJ_1717 : ์ง‘ํ•ฉ์˜ ํ‘œํ˜„  (0) 2023.03.08