메뉴 건너뛰기

Programing

BAEKJOON

9019

관리자 2019.12.21 19:23 조회 수 : 162

C++

#include <cstdio>

#include <queue>

#include <algorithm>

#include <cstring>

#include <cstdlib>

using namespace std;

struct Q_info{

    int n;

};

queue<Q_info> q;

int N;

char result[10010][10010];

int check[10010];

int memory[10010][2];

void ClearQueue(queue<Q_info> &someQueue)

{

    queue<Q_info> empty_;

    swap(someQueue, empty_);

}

void reverseString(char* s) {

  size_t size_ = strlen(s);

  char temp;

 

  for (size_t i = 0; i < size_ / 2; i++) {

    temp = s[i];

    s[i] = s[(size_ - 1) - i];

    s[(size_ - 1) - i] = temp;

  }

}

int main()

{

    scanf("%d",&N);

    for(int i=0;i<N;i++){

        int f,b;

        scanf("%d %d",&f,&b);

        ClearQueue(q);

        memset(check,0,sizeof(check));

        memset(memory,0,sizeof(memory));

        q.push({f});

        check[f]=1;

        while(!q.empty()){

            Q_info a=q.front();

            q.pop();

            if(a.n==b){

                //result[i]=a.path;

                break;

            }

            if(check[(a.n*2)%10000]==0){

                check[(a.n*2)%10000]=1;

                q.push({(a.n*2)%10000});//1==d

                memory[(a.n*2)%10000][0]=a.n;

                memory[(a.n*2)%10000][1]=1;

            }

            if(a.n==0){

                if(check[9999]==0){

                    check[9999]=1;

                    q.push({9999});

                    memory[9999][0]=a.n;

                    memory[9999][1]=2;

                }

            }else{

                if(check[a.n-1]==0){

                    check[a.n-1]=1;

                    q.push({a.n-1});

                    memory[a.n-1][0]=a.n;

                    memory[a.n-1][1]=2;

                }

            }

            int num=a.n%10;

            if(check[a.n/10+num*1000]==0){

                check[a.n/10+num*1000]=1;

                q.push({a.n/10+num*1000});

                memory[a.n/10+num*1000][0]=a.n;

                memory[a.n/10+num*1000][1]=4;

            }

            num=a.n/1000;

            if(check[a.n%1000*10+num]==0){

                check[a.n%1000*10+num]=1;

                q.push({a.n%1000*10+num});

                memory[a.n%1000*10+num][0]=a.n;

                memory[a.n%1000*10+num][1]=3;

            }

 

        }

        int idx=b;

        int idx_s=0;

        while(1){

            if(memory[idx][1]==0){

                break;

            }

            switch(memory[idx][1]){

            case 1:

                result[i][idx_s]='D';

                break;

            case 2:

                result[i][idx_s]='S';

                break;

            case 3:

                result[i][idx_s]='L';

                break;

            case 4:

                result[i][idx_s]='R';

                break;

            }

            idx_s++;

            idx=memory[idx][0];

        }

    }

    for(int i=0;i<N;i++){

        reverseString(result[i]);

        printf("%s\n",result[i]);

    }

    return 0;

}

번호 제목 글쓴이 날짜 조회 수
공지 안내사항 관리자 2019.12.21 215
74 9663 관리자 2019.12.21 150
73 9345 관리자 2020.04.11 195
» 9019 관리자 2019.12.21 162
71 7569 관리자 2019.12.21 159
70 6987 관리자 2019.12.21 152
69 5842 관리자 2020.04.11 186
68 5625 관리자 2019.12.21 159
67 5419 관리자 2020.04.11 201
66 3392 관리자 2020.04.11 195
65 3019 관리자 2019.12.21 151
64 2933 관리자 2019.12.21 152
63 2931 관리자 2019.12.21 150
62 2836 관리자 2019.12.21 146
61 2667 관리자 2019.12.21 148
60 2636 관리자 2019.12.21 137
59 2629 관리자 2020.04.11 178
58 2615 관리자 2019.12.21 145
57 2610 관리자 2020.04.11 181
56 2606 관리자 2019.12.21 148
55 2585 관리자 2019.12.21 154
54 2578 관리자 2019.12.21 142
53 2573 관리자 2020.04.11 190
52 2557 관리자 2019.12.21 154
51 2512 관리자 2019.12.21 149
50 2504 관리자 2020.04.11 178
49 2481 관리자 2019.12.21 145
48 2478 관리자 2020.04.11 184
47 2473 관리자 2020.04.11 189
46 2470 관리자 2020.04.11 192
위로