[ 백준 ] 9019 / DSLR

金弘均·2021년 9월 15일
0

Baekjoon Online Judge

목록 보기
109/228
post-thumbnail

# Appreciation

/*
 * Problem :: 9019 / DSLR
 *
 * Kind :: BFS
 *
 * Insight
 * - 최소한의 무엇을 구하라면 일단 BFS 로 생각해보자
 *   + 범위가 [0,10000) 이다. 이정도면 방문처리도 배열로 쉽게 할 수 있다
 *   + 중간 상태에 레지스터 값과 그 값을 만든 명령어의 나열도 같이 저장할 수 있지만,
 *     방문처리 하는 김에 이전의 레지스터 값이 무엇인지와
 *     현재 레지스터 값을 만든 명령어를 저장하는 배열을 각각 선언해서
 *     이를 이용했다 (코드 작성면에서 훨씬 깔끔한듯)
 *
 * Point
 * - d1, d2, d3, d4 따로 저장하기 보다는 이들의 수가 가리키는
 *   d = ((d1 * 10 + d2) * 10 + d3) + d4
 *   를 저장한 후, 이 d 로부터 d1, d2, d3, d4 를 필요할 때마다 선언해서 쓰는 것이 좋다
 */

# Code

//
//  BOJ
//  ver.C++
//
//  Created by GGlifer
//
//  Open Source

#include <iostream>
#include <queue>
#include <cstring>
#include <stack>

using namespace std;

#define endl '\n'

// Set up : Global Variables
/* None */

// Set up : Functions Declaration
/* None */


int main()
{
    // Set up : I/O
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // Set up : Input
    int T; cin >> T;

    while (T--) {
        int A, B;
        cin >> A >> B;

        // Process
        queue<int> que;
        bool isVisited[10000]; /* 방문 처리 위한 배열 */
        memset(isVisited, false, sizeof(isVisited));
        int p[10000]; /* 이전 값 처리 위한 배열 */
        memset(p, -1, sizeof(p));
        char m[10000]; /* 현재 값을 만든 명령어 처리 위한 배열 */
        memset(m, 'X', sizeof(m));

        que.push(A);
        isVisited[A] = true;

        while (not(que.empty())) {
            int c = que.front(); que.pop();
            /* Queue 에서 꺼낸 원소가 B 와 같으면 BFS 중단 */
            if (c == B) {
                break;
            }

            /* 현재 값으로 부터 d1, d2, d3, d4 를 도출 */
            int d4 = c % 10;
            int d3 = c / 10 % 10; /* 얘는 무쓸모 */
            int d2 = c / 100 % 10; /* 얘도 무쓸모 */
            int d1 = c / 1000 % 10;

            int v;
            /* 명령어 : D */
            v = (2 * c) % 10000;
            if (not(isVisited[v])) {
                isVisited[v] = true;
                p[v] = c;
                m[v] = 'D';
                que.push(v);
            }

            /* 명령어 : S */
            v = (c + 10000 - 1) % 10000;
            if (not(isVisited[v])) {
                isVisited[v] = true;
                p[v] = c;
                m[v] = 'S';
                que.push(v);
            }

            /* 명령어 : L */
            v = c % 1000 * 10 + d1;
            if (not(isVisited[v])) {
                isVisited[v] = true;
                p[v] = c;
                m[v] = 'L';
                que.push(v);
            }

            /* 명령어 : R */
            v = c / 10 + 1000 * d4;
            if (not(isVisited[v])) {
                isVisited[v] = true;
                p[v] = c;
                m[v] = 'R';
                que.push(v);
            }
        }

        // Control : Output
        stack<char> st;
        /* 현재 값의 이전 값이 -1 이 아닐 때까지 */
        while (p[B] != -1) {
            /* 현재 값을 만든 명령어를 스택에 추가 */
            st.push(m[B]);
            /* 현재 값을 이전 값으로 갱신 */
            B = p[B];
        }
        /* 값 B를 만든 명령어들 중 최근에 사용한 순서의 명령어부터 역순으로 출력 */
        while (not(st.empty())) {
            cout << st.top(); st.pop();
        } cout << endl;
    }
}

// Helper Functions
/* None */
profile
이런 미친 게임을 봤나! - 옥냥이

0개의 댓글