나의 브을로오그으

[c++] 9019번 : DSLR 본문

알고리즘/BaekJoon

[c++] 9019번 : DSLR

__jhp_+ 2022. 10. 27. 16:27

https://www.acmicpc.net/problem/9019

 

9019번: DSLR

네 개의 명령어 D, S, L, R 을 이용하는 간단한 계산기가 있다. 이 계산기에는 레지스터가 하나 있는데, 이 레지스터에는 0 이상 10,000 미만의 십진수를 저장할 수 있다. 각 명령어는 이 레지스터에

www.acmicpc.net

#include <iostream>
#include <queue>
#include <string>

using namespace std;

string Progress(int n, int ans)
{
	bool visited[10000] = { false, };
	queue<pair<int, string>> q;
	q.emplace(n, "");
	visited[n] = true;

	while (q.empty() == false)
	{
		int cur = q.front().first;
		string cmd = q.front().second;
		q.pop();
		if (cur == ans)
		{
			return cmd;
		}

		int D, S, L, R;
		D = cur * 2 % 10000;
		S = cur - 1 < 0 ? 9999 : cur - 1;
		L = cur % 1000 * 10 + cur / 1000;
		R = cur % 10 * 1000 + cur / 10;

		if (visited[D] == false)
		{
			visited[D] = true;
			q.emplace(D, cmd + "D");
		}

		if (visited[S] == false)
		{
			visited[S] = true;
			q.emplace(S, cmd + "S");
		}

		if (visited[L] == false)
		{
			visited[L] = true;
			q.emplace(L, cmd + "L");
		}

		if (visited[R] == false)
		{
			visited[R] = true;
			q.emplace(R, cmd + "R");
		}
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	vector<int> cmdLine;
	int T, n, ans;
	cin >> T;
	for (int i = 0; i < T; ++i)
	{
		cin >> n >> ans;
		string cmdLine = Progress(n, ans);
		cout << cmdLine << '\n';
	}

	return 0;
}

'알고리즘 > BaekJoon' 카테고리의 다른 글

[c++] 10026번 : 적록색약  (0) 2022.11.16
[c++] 7662번 : 이중 우선순위 큐  (0) 2022.10.26
[c++] 7576번 : 토마토  (0) 2022.10.21
[c++] 7569번 : 토마토  (0) 2022.10.19
[c++] 18870번 : 좌표 압축  (0) 2022.10.12