• 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;
}