반응형
https://www.acmicpc.net/problem/9019
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<queue>
#include<vector>
#include<string.h>
using namespace std;
int sstart, eend;
vector<char> v;
int times;
int visited[10001];
void print(vector<char> calcul) {
for (int i = 0; i < calcul.size(); i++) {
printf("%c", calcul[i]);
}
}
void bfs(int ss, int e) {
queue <pair<int, vector<char> > > q;
q.push(make_pair( ss, v ));
//초기화
visited[ss] = 1;
vector<char> d, s, l, r;
while (!q.empty()) {
int number = q.front().first;
d=s=l=r = q.front().second;
q.pop();
int val = (number * 2) % 10000;
if (visited[val] == 0) {
d.push_back('D');
if (val == eend) {
print(d);
return;
}
q.push(make_pair(val, d ));
visited[val] = 1;
}
val = (number + 9999) % 10000;
if (visited[val] == 0) {
s.push_back('S');
if (val == eend) {
print(s);
return;
}
q.push(make_pair(val, s ));
visited[val] = 1;
}
val = (((number % 1000) * 10) + (number / 1000));
if (visited[val] == 0) {
l.push_back('L');
if (val == eend) {
print(l);
return;
}
q.push(make_pair(val, l ));
visited[val] = 1;
}
val = (number % 10 * 1000) + (number / 10);
if (visited[val] == 0) {
r.push_back('R');
if (val == eend) {
print(r);
return;
}
q.push(make_pair(val, r ));
visited[val] = 1;
}
}
}
int main() {
int testcase;
scanf("%d", &testcase);
for (int t = 0; t < testcase; t++) {
memset(visited, 0, sizeof(visited));
scanf("%d%d", &sstart, &eend);
bfs(sstart, eend);
printf("\n");
}
return 0;
}
반응형
'CS > 알고리즘' 카테고리의 다른 글
백준 17142 연구소3(bfs dfs) (0) | 2019.10.27 |
---|---|
백준 16234 인구이동(bfs) (0) | 2019.10.27 |
백준 3055 탈출 (0) | 2019.10.27 |
백준 1987 알파벳(dfs)_알파벳 아스키코드 (0) | 2019.10.27 |
백준 2206 벽 부수고 이동하기 (0) | 2019.10.27 |