๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’ป Computer Science/- Data Structure, Algorithm

[์•Œ๊ณ ๋ฆฌ์ฆ˜ PS] ๋ฐฑ์ค€ 9019๋ฒˆ DSLR ์ž๋ฐ” ๋ฌธ์ œํ’€์ด

by Wonit 2021. 8. 13.

๋ฌธ์ œ

ํ•ด๋‹น ํฌ์ŠคํŒ…์€ ๋ฐฑ์ค€์˜ 9019๋ฒˆ DSLR ์˜ ์ ‘๊ทผ๊ณผ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค.
์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

9019๋ฒˆ: DSLR

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

www.acmicpc.net

 

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ๋จผ์ € ์ƒ๊ฐํ•ด๋ณด์ž.

 

๋ฌธ์ œ ์ ‘๊ทผ

 

์ด๋ฒˆ ๋ฌธ์ œ๋Š” BFS ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ์žฌ๋ฏธ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

์ด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ ๊ฑฐ์˜ 3์‹œ๊ฐ„์€ ์Ÿ์•˜๋Š”๋ฐ.. ๊ณ„์†ํ•ด์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋‚ฌ๋˜ ๋ฌธ์ œ๋‹ค ใ… ใ… 

 

ํ•ด๊ฒฐ๋ฒ•

 

1 ~ 1๋งŒ ๊นŒ์ง€ ์ˆซ์ž๊ฐ€ ์กด์žฌํ•  ๋•Œ, 4๊ฐ€์ง€ ์—ฐ์‚ฐ์„ ๋ชจ๋‘ ์ˆ˜ํ–‰ํ•ด๋ณด๋ฉด์„œ ์ตœ์†Œ ํšŸ์ˆ˜๋ฅผ ์„ธ๋ฉด ๋œ๋‹ค.

 

์ฒ˜์Œ์—๋Š” L๊ณผ R ์—ฐ์‚ฐ์—์„œ Shift ์—ฐ์‚ฐ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ String ํด๋ž˜์Šค์™€ Arrays.stream() ์„ ํ•จ๊ป˜ ์ด์šฉํ–ˆ์—ˆ๋Š”๋ฐ, ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๊ณ  ๊ฒฐ๊ตญ ๋ธ”๋กœ๊ทธ์˜ ๋„์›€์„ ๋ฐ›์•„์„œ shift ์—ฐ์‚ฐ์„ ์ˆ˜ํ•™์ ์œผ๋กœ ํ•ด๊ฒฐํ–ˆ๋˜ ๋ฌธ์ œ์ด๋‹ค.

 

์ •๋‹ต ์ฝ”๋“œ

public class Main {

    private static int[] depth;
    private static int a;
    private static int b;

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

        int t = Integer.parseInt(br.readLine());

        while(t-- > 0) {

            String[] ab = br.readLine().split(" ");

            a = Integer.parseInt(ab[0]);
            b = Integer.parseInt(ab[1]);

            depth = new int[10000];

            bw.write(getCommand() + "\n");
        }

        bw.flush();
        bw.close();
    }

    private static String getCommand() {
        Queue<Pair> queue = new LinkedList<>();

        queue.add(new Pair("", a));
        depth[a] = 1;

        while(!queue.isEmpty()) {
            Pair front = queue.remove();

            if(front.number == b) {
                return front.command;
            }

            int l = l(front.number);
            if(depth[l] == 0) {
                depth[l] = depth[front.number] + 1;
                queue.add(new Pair(front.command+"L", l));
            }

            int r = r(front.number);
            if(depth[r] == 0) {
                depth[r] = depth[front.number] + 1;
                queue.add(new Pair(front.command+"R", r));
            }

            int d = d(front.number);
            if(depth[d] == 0) {
                depth[d] = depth[front.number] + 1;
                queue.add(new Pair(front.command+"D", d));
            }

            int s = s(front.number);
            if(depth[s] == 0) {
                depth[s] = depth[front.number] + 1;
                queue.add(new Pair(front.command+"S", s));
            }
        }
        return null;
    }

    private static int d(int n) {
        n *= 2;
        if(9999 < n) n %= 10_000;
        return n;
    }

    private static int s(int n) {
        if(n == 0) n = 9999;
        else n -= 1;
        return n;
    }

    private static int l(int n) {
        return n % 1000 * 10 + n / 1000;
    }

    private static int r(int n) {
        return n % 10 * 1000 + n / 10;
    }
}

class Pair {
    String command;
    int number;

    public Pair(String command, int number) {
        this.command = command;
        this.number = number;
    }
}

์–ด๋–ค ๊ฐ’์ด๋˜ 1 ~ 1๋งŒ ์‚ฌ์ด๋กœ๋Š” ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•ด์„œ return ์„ null ๋กœ ํ•ด์ค˜๋„ ํฌ๊ฒŒ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค.


์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋Œ“๊ธ€