c++의 ranged-based for문에 익숙해지니 이따금 c++이 선녀같아 보이는 마법에 빠졌다...
#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(0), cin.tie(0)
using namespace std;
bool visited[10000];
int src, dst, t;
void input() {
fastio;
cin >> t;
}
string bfs() {
memset(visited, false, sizeof(visited));
queue<pair<int, string> > q;
visited[src] = true;
q.push({src, ""});
while(!q.empty()) {
auto [n, str] = q.front(); q.pop();
if(n == dst) return str;
int d = (n * 2) % 10000;
if(!visited[d]) q.push({d, str+'D'});
int s = n ? n-1 : 9999;
if(!visited[s]) q.push({s, str+'S'});
int l = (n%1000)*10 + (n/1000);
if(!visited[l]) q.push({l, str+'L'});
int r = (n/10) + (n%10)*1000;
if(!visited[r]) q.push({r, str+'R'});
for(int next : {d, s, l, r}) visited[next] = true;
}
return "";
}
int main() {
input();
while(t--) {
cin >> src >> dst;
cout << bfs() << '\n';
}
}
백준 3665 최종 순위 혼내주기 (0) | 2021.07.26 |
---|---|
백준 14500 테트로미노 혼내주기 (0) | 2021.07.26 |
백준 16236 아기 상어 혼내주기 (0) | 2021.07.26 |
백준 17069, 17070 파이프 옮기기 혼내주기 (0) | 2021.07.26 |
백준 2407 조합 혼내주기 (0) | 2021.07.26 |
댓글 영역