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