๋์ด๋ : ๊ณจ๋ 5
๊ฑธ๋ฆฐ ์๊ฐ : 1์๊ฐ 30๋ถ
๋ฌธ์
ํ์ด
- DSLR์ด๋ผ๋ 4๊ฐ์ง ๊ฒฝ๋ก๊ฐ ์๋ค๊ณ ์๊ฐํ๊ณ bfs
์ฝ๋
#include <iostream>
#include <queue>
#include <string>
using namespace std;
struct point
{
int prev;
char state;
int visited = -1;
};
char dslrArr[4] = { 'D', 'S', 'L', 'R' };
int dslrResult(int type, int num) {
int tmp;
switch (type) {
case 0 :
return (num* 2) % 10000;
case 1 :
if (num == 0)
return 9999;
else
return num - 1;
case 2 :
tmp = num / 1000; // pow ์ฌ์ฉ x => ์๊ฐ์ด๊ณผ
return (num - tmp * 1000) * 10 + tmp;
case 3 :
return (num % 10) * 1000 + num / 10;
}
}
void getAnswer(vector<char>& ans, point* memo, int a, int b) { // string์ ๋ถ์ด๋ ์์ผ๋ก => ์๊ฐ์ด๊ณผ
int idx = b;
while (1){
if (idx == a) break;
ans.push_back(memo[idx].state);
idx = memo[idx].prev;
}
}
int main() {
int T; cin >> T;
while (T--) {
int A; int B;
cin >> A >> B;
int flag = 0;
vector<char> answer;
point memo[10001];
queue<int> que;
que.push(A); // ์ด๊ธฐํ ์ ๋ฃ๊ธฐ
memo[A].visited = 1;
while (!que.empty()) {
int tmp = que.front(); que.pop();
for (int i = 0; i < 4; i++) {
if (tmp == 0 && i != 1) // 0์ ๊ณฑ์
์ฐ์ฐ ์๋ฏธ x => ์๊ฐ ๊ฐ์
continue;
int res = dslrResult(i, tmp);
if (memo[res].visited != -1) { // ์ด๋ฏธ ๋ค๋ฅธ ๊ณณ
continue;
}
memo[res].prev = tmp;
memo[res].state = dslrArr[i];
memo[res].visited = 1;
if (res == B) {
// B๊ฐ ๋์ด
getAnswer(answer, memo, A, B);
flag = 1;
break;
} else {
que.push(res);
}
}
if (flag)
break;
}
for (int i=answer.size()-1; i >= 0; i--) {
cout << answer[i];
}
cout << '\n';
}
}
์ฃผ์ํ ์
- pow ์ฐ์ฐ์ ์๊ฐ๋ณด๋ค ๋๋ฆฌ๋ค => ์์๋ก ์ฌ์ฉํ ์ ์๋ค๋ฉด ์์๋ก ์ฌ์ฉํ์.
- string ์ด์ด๋ถ์ด๊ธฐ ์ฐ์ฐ์ ์๊ฐ๋ณด๋ค ๋๋ฆฌ๋ค => vector๋ฅผ ์ด์ฉํ์ฌ ์ ์ฅํ๊ณ ์ ๊ทผํ๋ ๊ฒ์ด ๋ ๋น ๋ฅด๋ค.
- ๋ค๋ฅธ ์ฝ๋๋ค ๋ณด๋๊น string ์ด์ด๋ถ์ด๊ธฐ ์ฐ์ฐ์ ๊ด์ฐฎ์ ๋ฏํ๋ค.
- ํ ์คํธ์ผ์ด์ค๊ฐ ์๋ ๋ฌธ์ ๋ ์ด๊ธฐํ์ ์ ๊ฒฝ์ฐ์.
๊ฒฐ๊ณผ
์์ด๋ | ๋ฉ๋ชจ๋ฆฌ | ์๊ฐ | ์ธ์ด | ์ฝ๋ ๊ธธ์ด |
---|---|---|---|---|
gmldms784 | 2024 | 1512 | C++17 / ์์ | 1843 |
๋ฐ์ํ
'Algorithm ๋ฌธ์ > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[c++] BOJ 2250 :: ํธ๋ฆฌ์ ๋์ด์ ๋๋น (0) | 2021.06.02 |
---|---|
[c++] BOJ 1525 :: ํผ์ฆ (0) | 2021.05.26 |
[c++] BOJ 2146 :: ๋ค๋ฆฌ ๋ง๋ค๊ธฐ (0) | 2021.05.18 |
[c++] BOJ 2589 :: ๋ณด๋ฌผ์ฌ (0) | 2021.05.18 |
[c++] BOJ 1707 :: ์ด๋ถ ๊ทธ๋ํ (0) | 2021.05.12 |
Comment