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

github : https://github.com/junho0956/Algorithm/blob/master/9019/9019/%EC%86%8C%EC%8A%A4.cpp

 

문제에서 요구하는 그대로 작성하였다.

코드를 정확히 하지않으면 1000 같은 숫자는 L 했을 때 1 이 되고 R 했을 때 100 이 되는 경우들에 대한

반례가 분명 생길 것 같아서

어차피 4자리 숫자이니 한자리씩 가지고 와서 체크후, BFS 해도 괜찮을 것 같았다.

코드에 대한 설명은 주석처리 해놓았다.

 

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
 
// 한자리씩 가지고오기 위한 구조체
typedef struct number {
    int init[4];
};
number result;
 
// 어떤 명령어를 사용했는지 체크하기 위한 배열
pair<charint> parent[10000];
bool visit[10000];
int Testcase, A, B, now;
queue<int> q;
stack<char> s;
 
void now_check(int num) {
 
    for (int i = 0; i < 4; i++result.init[i] = 0;
 
    int idx = 3;
    while (num) {
        result.init[idx--= num % 10;
        num /= 10;
    }
}
 
void D() {
    int temp = result.init[0];
    for (int i = 1; i < 4; i++) {
        temp = temp * 10 + result.init[i];
    }
 
    temp = (temp * 2) % 10000;
    if (!visit[temp] && parent[temp].first == NULL) {
        parent[temp].first = 'D';
        parent[temp].second = now;
        q.push(temp);
    }
}
 
void S() {
    int temp = result.init[0];
    for (int i = 1; i < 4; i++) {
        temp = temp * 10 + result.init[i];
    }
 
    if (temp == 0) temp = 9999;
    else temp--;
 
    if (!visit[temp] && parent[temp].first == NULL) {
        parent[temp].first = 'S';
        parent[temp].second = now;
        q.push(temp);
    }
}
 
void L() {
    int temp = result.init[1];
    temp = temp * 10 + result.init[2];
    temp = temp * 10 + result.init[3];
    temp = temp * 10 + result.init[0];
 
    if (!visit[temp] && parent[temp].first == NULL) {
        parent[temp].first = 'L';
        parent[temp].second = now;
        q.push(temp);
    }
}
 
void R() {
    int temp = result.init[3];
    temp = temp * 10 + result.init[0];
    temp = temp * 10 + result.init[1];
    temp = temp * 10 + result.init[2];
 
    if (!visit[temp] && parent[temp].first == NULL) {
        parent[temp].first = 'R';
        parent[temp].second = now;
        q.push(temp);
    }
}
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
 
    cin >> Testcase;
    while (Testcase--) {
        for (int i = 0; i < 10000; i++) parent[i].first = NULL;
        for (int i = 0; i < 10000; i++) visit[i] = 0;
 
        cin >> A >> B;
        q.push(A);
 
        while (!q.empty()) {
            now = q.front();
            q.pop();
 
            if (visit[now]) continue;
            visit[now] = true;
            
            if (now == B) {
                while (!q.empty()) q.pop();
                break;
            }
// now_check 를 통해서 구조체에 한자리씩 저장해둔다
            now_check(now);
// 주어진 조건에 맞게 함수내에서 q push
            D(), S(), L(), R();
        }
 
        int temp = B;
// 부모를 따라가는 과정
        while (parent[temp].first != NULL) {
            s.push(parent[temp].first);
            temp = parent[temp].second;
        }
        while (!s.empty()) {
            cout << s.top();
            s.pop();
        }
        cout << '\n';
    }
 
    return 0;
}
 
 
 

'algorithm > BOJ' 카테고리의 다른 글

BOJ 2251번 물통  (0) 2020.01.24
BOJ 1525번 퍼즐  (0) 2020.01.24
BOJ 1963번 소수 경로  (0) 2020.01.23
BOJ 13913번 숨바꼭질4  (0) 2020.01.23
BOJ 13549번 숨바꼭질3  (0) 2020.01.22

+ Recent posts