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