• toc {:toc}

๐Ÿ“ฆ9019: DSLR


[์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด] ๊ฐ•๋™๊ทœ
Reviewed by Kade Kang (devkade12@gmail.com)
Reviewed:: 2024-02-05
๋ฌธ์ œ ํ™•์ธํ•˜๊ธฐ

ํ’€์ด

ํ—˜๋‚œํ•œ ํ’€์ด์˜€๋‹ค. ๊ณ„์† 3%์—์„œ ํ‹€๋ ค์„œ ๋ฌด์—‡์ด ๋ฌธ์ œ์ธ๊ฐ€ ๊ณ„์† ๊ณ ๋ฏผํ–ˆ๋‹ค. ์ด์ „์˜ ๊ฒฝ์šฐ์—๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ if๋ฌธ์œผ๋กœ ํ•˜๋‚˜ํ•˜๋‚˜ ์ฒ˜๋ฆฌํ•œ ํ˜•ํƒœ์˜€๊ณ , !!! ์œผ๋กœ ํ‘œ์‹œํ•ด๋‘” ๋ถ€๋ถ„์—์„œ temp๊ฐ€ ์ •์˜๋˜์ง€ ์•Š์€ ์ƒํƒœ์—์„œ temp == 0 ๋น„๊ต๋ฅผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. @@@๋กœ ๋ณ€๊ฒฝํ–ˆ์„ ๋•Œ์—๋Š” ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•ด ์•„๋ž˜ ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝํ–ˆ๋‹ค.

#include <iostream>
#include <vector>
#include <queue>
#include <string>
#define endl '\n';
#define X first;
#define Y second;
using namespace std;
 
char remote[4] = {'D', 'S', 'L', 'R'};
int start, target;
queue<pair<string, int>> q;
int vis[10001];
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    int T;
    cin >> T;
 
    while(T--){
        while(!q.empty()) q.pop();
        fill(vis, vis+10001, 0);
 
        cin >> start >> target;
        q.push({"", start});
        vis[start] = 1;
        while(!q.empty()){
            auto info = q.front(); q.pop();
            int val = info.Y;
 
            if(val == target){
                cout << info.first << endl;
                break;
            }
            
            for(int i = 0; i < 4; i++){
                int temp;
                string cmd = info.X;
 
                if (remote[i] == 'D'){
                    temp = (2 * val) % 10000;
                }
                else if (remote[i] == 'S'){
					// if (temp == 0) temp = 9999;                     // !!!
                    // else temp = val - 1;
					 temp = (val == 0) ? 9999 : val - 1;              // @@@
                }
                else if (remote[i] == 'L'){
                    temp = (val % 1000) * 10 + (val / 1000);
                }
                else{
                    temp = (val / 10) + 1000 * (val % 10);
                }
 
                cmd = cmd + remote[i];
 
                if(!vis[temp]){
                    vis[temp] = 1;
                    q.push({cmd, temp});
                }
            }
        }
    }
 
    return 0;
}

์•„๋ฌด๋ž˜๋„ for๋ฌธ๊ณผ if๋ฌธ์œผ๋กœ ์ธํ•ด์„œ for๋ฌธ ์ง„ํ–‰ํ•˜๋ฉด์„œ if๋ฌธ์ด ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฐ˜๋ณต๋˜๋Š” ๊ฒƒ์œผ๋กœ ์ธํ•ด์„œ ์ถ”๊ฐ€์ ์ธ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

#include <iostream>
#include <queue>
#include <string>
#include <cstring>
#define endl '\n';
#define X first;
#define Y second;
using namespace std;
 
int start, target;
int vis[10001];
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    int T;
    cin >> T;
 
    while(T--){
        queue<pair<string, int>> q;
        memset(vis, 0, sizeof(vis));
 
        cin >> start >> target;
        q.push({"", start});
        vis[start] = 1;
        while(!q.empty()){
            auto info = q.front(); q.pop();
            int val = info.Y;
 
            if(val == target){
                cout << info.first << endl;
                break;
            }
            
            int temp;
            string cmd = info.X;
 
            temp = (2 * val) % 10000;
            if(!vis[temp]){
                vis[temp] = 1;
                q.push({cmd + "D", temp});
            }
            temp = (val == 0) ? 9999 : val - 1;
            if(!vis[temp]){
                vis[temp] = 1;
                q.push({cmd + "S", temp});
            }
            temp = (val % 1000) * 10 + (val / 1000);
            if(!vis[temp]){
                vis[temp] = 1;
                q.push({cmd + "L", temp});
            }
            temp = (val / 10) + 1000 * (val % 10);
            if(!vis[temp]){
                vis[temp] = 1;
                q.push({cmd + "R", temp});
            }
        }
    }
 
    return 0;
}