Henu
개발냥발
Henu
전체 방문자
오늘
어제
  • 분류 전체보기 (411)
    • DevOps (52)
      • Kubernetes (19)
      • Docker (14)
      • AWS (3)
      • Nginx (4)
      • Linux (4)
      • ArgoCD (1)
      • CN (2)
      • NATS (0)
      • Git (5)
    • Back-End (30)
      • Django (18)
      • Spring (5)
      • JPA (1)
      • MSA (5)
    • CS (87)
      • SystemSoftware (20)
      • OS (25)
      • Computer Architecture (16)
      • Network (23)
      • Database (2)
    • Lang (21)
      • Java (9)
      • Python (4)
      • C# (8)
    • Life (12)
    • 블록체인 (2)
    • Algorithm (204)
      • BOJ (160)
      • 프로그래머스 (19)
      • LeetCode (4)
      • SWEA (1)
      • 알고리즘 문제 해결 전략 (8)
      • DS, algorithms (7)
      • Checkio (5)
    • IT (2)

블로그 메뉴

  • GitHub
  • 글쓰기
  • 관리자

공지사항

  • Free!

인기 글

태그

  • BFS
  • docker
  • 백트래킹
  • Network
  • 다이나믹 프로그래밍
  • 프로그래머스
  • Kubernetes
  • DFS
  • boj
  • django

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Henu

개발냥발

[백준 9019] DSLR (C++)
Algorithm/BOJ

[백준 9019] DSLR (C++)

2021. 7. 30. 21:12
 

9019번: DSLR

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

www.acmicpc.net

BFS


이미 찾아 본 숫자라면 que에 넣지않도록해서 최대 10000번만 bfs를 수행하도록 했다.

메모이제이션 안하면 4^n 의 시간복잡도로 시간초과가 난다.

 

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
76
77
78
79
80
81
82
83
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
#define fasti ios_base::sync_with_stdio(false); cin.tie(0);
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 1e9+7
#define pii pair<int, int>
#define MOD 10000
typedef long long ll;
// typedef pair<int, int> pii;
 
using namespace std;
 
int T, A, B;
 
int D(int now){
    return (now*2) % MOD;
}
int S(int now){
    if(!now) return 9999;
    return now-1;
}
int L(int now){
    int k = now/1000;
    return (now%1000)*10+ k;
}
int R(int now){
    int k = now%10;
    return (now/10) + k*1000;
}
 
string bfs(int source, int sink){
    queue<pair<int, string> > que;
    que.push({source, ""});
    bool visited[10001];
    memset(visited, 0, sizeof(visited));
    
    while(!que.empty()){
        int now = que.front().first;
        string str = que.front().second;
        que.pop();
        
        int d = D(now);
        if(!visited[d]) que.push({d, str+'D'});
        visited[d] = true;
        if(d == sink) return str+'D';
        
        int s = S(now);
        if(!visited[s]) que.push({s, str+'S'});
        visited[s] = true;
        if(s == sink) return str+'S';
        
        int l = L(now);
        if(!visited[l]) que.push({l, str+'L'});
        visited[l] = true;
        if(l == sink) return str+'L';
        
        int r = R(now);
        if(!visited[r]) que.push({r, str+'R'});
        visited[r] = true;
        if(r == sink) return str+'R';
    }
    return "";
}
 
void solve(){
    cin >> T;
    for(int i = 0; i < T; i++){
        cin >> A >> B;
        cout << bfs(A, B) << "\n";
    }
    
}
 
int main(){
    fastio
    solve();
    
    return 0;
}
 
Colored by Color Scripter
cs

 

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

[백준 1701] Cubeditor (C++)  (0) 2021.07.31
[백준 1275] 커피숍2 (C++)  (0) 2021.07.31
[백준 2188] 축사 배정 (C++)  (0) 2021.07.30
[백준 11505] 구간 곱 구하기 (C++)  (0) 2021.07.29
[백준 1305] 광고 C++  (0) 2021.07.28
    'Algorithm/BOJ' 카테고리의 다른 글
    • [백준 1701] Cubeditor (C++)
    • [백준 1275] 커피숍2 (C++)
    • [백준 2188] 축사 배정 (C++)
    • [백준 11505] 구간 곱 구하기 (C++)

    티스토리툴바