ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [백준] DSLR - 9019번
    알고리즘 문제 풀이/백준 2021. 6. 23. 20:57

    문제설명

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

     

    9019번: DSLR

    네 개의 명령어 D, S, L, R 을 이용하는 간단한 계산기가 있다. 이 계산기에는 레지스터가 하나 있는데, 이 레지스터에는 0 이상 10,000 미만의 십진수를 저장할 수 있다. 각 명령어는 이 레지스터에

    www.acmicpc.net

     

     

     

    기본아이디어

    D, S, L, R 명령어를 가진 계산기로 주어진 A 값을 B로 만드는 최소한의 명령어를 생성하는 문제입니다. 일단 최소한의 명령어를 사용한다는 규칙을 보고 먼저 BFS 탐색을 해야겠다 라고 먼저 생각을 하고 접근했습니다.

     

    그러고 나서 D, S, L, R 명령어로 값을 계산해주는 함수를 만들었습니다.

     

    구현코드

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
        public static void main(String[] args) throws IOException {
            int T = Integer.parseInt(input.readLine());
            for(int test_case = 1; test_case <= T; test_case++){
                StringTokenizer st = new StringTokenizer(input.readLine());
                A = Integer.parseInt(st.nextToken());
                B = Integer.parseInt(st.nextToken());
                bfs();
            }
            System.out.println(output);
        }
        static int A, B;
        static class Calc{
            int num;
            String oper;
     
            public Calc(int n, String op){
                this.num = n;
                this.oper = op;
            }
        }
        static void bfs(){
            Queue<Calc> queue = new LinkedList<>();
            Calc a = new Calc(A, "");
            queue.add(a);
            boolean[] visited = new boolean[10000];
            visited[a.num] = true;
            while(!queue.isEmpty()){
                Calc temp = queue.poll();
                if(temp.num == B){
                    output.append(temp.oper + "\n");
                    return;
                }
     
                int d = D(temp.num);
                if(!visited[d]){
                    visited[d] = true;
                    queue.add(new Calc(d, temp.oper + "D"));
                }
                d = S(temp.num);
                if(!visited[d]){
                    visited[d] = true;
                    queue.add(new Calc(d, temp.oper + "S"));
                }
                d = L(temp.num);
                if(!visited[d]){
                    visited[d] = true;
                    queue.add(new Calc(d, temp.oper + "L"));
                }
                d = R(temp.num);
                if(!visited[d]){
                    visited[d] = true;
                    queue.add(new Calc(d, temp.oper + "R"));
                }
            }
        }
     
        static int D(int num){
            return (num*2) % 10000;
        }
        static int S(int num){
            int ans = num - 1;
            if(ans < 0)
                return 9999;
            return ans;
        }
        static int L(int num){
            int ans = (num - (num/1000)*1000* 10 + (num / 1000);
            return ans;
        }
        static int R(int num){
            int ans = num / 10 + (num % 10* 1000;
            return ans;
        }
     
     
    cs

     

     

    정리, 기억할 내용

    1. 최소한, 제일 먼저 -> BFS, 다익스트라 생각2. 문제 파악은 꼼꼼히, 개발은 빠르게

     

     

    '알고리즘 문제 풀이 > 백준' 카테고리의 다른 글

    [백준] 인구 이동 - 16234번  (0) 2021.06.28
    [백준] 리모컨 - 1107번  (0) 2021.06.28
    [백준] AC - 5430번  (0) 2021.06.23
    [백준] 감시 - 15683번  (0) 2021.04.21
    [백준] 다리 놓기 - 1010번  (0) 2021.04.16

    댓글

Designed by Tistory.