ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • BOJ 9019 [DSLR]
    알고리즘 풀이/BFS(너비 우선 탐색) 2018. 9. 18. 04:11

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


    안녕하세요. 이번 문제는 백준 온라인 저지의 DSLR입니다.


    문제 해결방법은 다음과 같습니다.



    1. 노드 생성(레지스터에 저장되는 10진수, 처음 숫자로부터 해당 10진수까지 가는 명령어의 경로)


    2. 각 명령어(D, S, L, R)로 인해 생성되는 정수와 경로를 큐에 저장.


    3. 목표 정수가 나오면 리턴.




    위와 같은 방법을 사용했더니, 문제는 통과했지만, 메모리와 시간을 많이 사용했습니다.


    더 효율적인 방법이 있는지 찾아봐야겠습니다.





    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
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    #include <iostream>
    #include <queue>
    using namespace std;
    class Node {
    public:
        int Num;
        string str;
        Node(int n, string s) {
            Num = n;
            str = s;
        }
    };
    queue<Node> q;
    int ch[4];
    string str_temp;
    int ori, dest;
    bool Visited[10000];
    int shift_reg(int num, int command) {
        int result = 0;
        ch[0= num / 1000;
        ch[1= (num - ch[0* 1000/ 100;
        ch[2= (num - ch[0* 1000 - ch[1* 100/ 10;
        ch[3= num - ch[0* 1000 - ch[1* 100 - ch[2* 10;
        switch(command) {
            case 0: { //left <<
                int temp = ch[0];
                ch[0= ch[1];
                ch[1= ch[2];
                ch[2= ch[3];
                ch[3= temp;
                break;
            }
            case 1: { // right >>
                int temp = ch[3];
                ch[3= ch[2];
                ch[2= ch[1];
                ch[1= ch[0];
                ch[0= temp;
                break;
            }
        }
        result = ch[0* 1000 + ch[1* 100 + ch[2* 10 + ch[3];
        return result;
    }
    string BFS() {
        while(!q.empty()) {
            Node node = q.front();
            q.pop();
            int num = node.Num;
            if(num == dest) {
                while(!q.empty())
                    q.pop();
                return node.str;
            }
            if(!Visited[(num * 2) % 10000]) {
                Visited[(num * 2) % 10000= true;
                string str = node.str + "D";
                Node new_node((num * 2) % 10000, str);
                q.push(new_node);
            }
            int temp = 0;
            if(num - 1 < 0)
                temp = 9999;
            else
                temp = num - 1;
            if(!Visited[temp]) {
                Visited[temp] = true;
                string str = node.str + "S";
                Node new_node(temp, str);
                q.push(new_node);
            }
            int left_num = shift_reg(num, 0);
            int right_num = shift_reg(num, 1);
            if(!Visited[left_num]) {
                Visited[left_num] = true;
                string str = node.str + "L";
                Node new_node(left_num, str);
                q.push(new_node);
            }
            if(!Visited[right_num]) {
                Visited[right_num] = true;
                string str = node.str + "R";
                Node new_node(right_num , str);
                q.push(new_node);
            }
            
        }
        return "";
    }
    int main() {
        int T;
        cin >> T;
        for(int test_case = 1; test_case <= T; test_case++) {
            cin >> ori >> dest;
            Node first_node(ori, "");
            q.push(first_node);
            Visited[ori] = true;
            cout << BFS() << endl;
            for(int i=0; i<10000; i++)
                Visited[i] = false;
        }
        return 0;
    }
    cs


    반응형

    '알고리즘 풀이 > BFS(너비 우선 탐색)' 카테고리의 다른 글

    BOJ 2146 [다리 만들기]  (2) 2018.10.04
    BOJ 1726 [로봇]  (0) 2018.10.04
    BOJ 2234 [성곽]  (0) 2018.10.02
    BOJ 1600 [말이 되고픈 원숭이]  (3) 2018.09.23
    SWEA 5653 [줄기세포배양]  (3) 2018.09.22

    댓글

Designed by Tistory.