๐Ÿ™‡โ€โ™€๏ธ[Gold IV] DSLR - 9019

๋ฌธ์ œ ๋งํฌ

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 2288 KB, ์‹œ๊ฐ„: 4168 ms

๋ถ„๋ฅ˜

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰, ๊ทธ๋ž˜ํ”„ ์ด๋ก , ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰

์ œ์ถœ ์ผ์ž

2024๋…„ 3์›” 25์ผ 19:22:21

๋ฌธ์ œ ์„ค๋ช…

๋„ค ๊ฐœ์˜ ๋ช…๋ น์–ด D, S, L, R ์„ ์ด์šฉํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ๊ธฐ๊ฐ€ ์žˆ๋‹ค. ์ด ๊ณ„์‚ฐ๊ธฐ์—๋Š” ๋ ˆ์ง€์Šคํ„ฐ๊ฐ€ ํ•˜๋‚˜ ์žˆ๋Š”๋ฐ, ์ด ๋ ˆ์ง€์Šคํ„ฐ์—๋Š” 0 ์ด์ƒ 10,000 ๋ฏธ๋งŒ์˜ ์‹ญ์ง„์ˆ˜๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ ๋ช…๋ น์–ด๋Š” ์ด ๋ ˆ์ง€์Šคํ„ฐ์— ์ €์žฅ๋œ n์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€ํ™˜ํ•œ๋‹ค. n์˜ ๋„ค ์ž๋ฆฟ์ˆ˜๋ฅผ d1, d2, d3, d4๋ผ๊ณ  ํ•˜์ž(์ฆ‰ n = ((d1 ร— 10 + d2) ร— 10 + d3) ร— 10 + d4๋ผ๊ณ  ํ•˜์ž)

  1. D: D ๋Š” n์„ ๋‘ ๋ฐฐ๋กœ ๋ฐ”๊พผ๋‹ค. ๊ฒฐ๊ณผ ๊ฐ’์ด 9999 ๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ์—๋Š” 10000 ์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ทจํ•œ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ๊ฐ’(2n mod 10000)์„ ๋ ˆ์ง€์Šคํ„ฐ์— ์ €์žฅํ•œ๋‹ค.
  2. S: S ๋Š” n์—์„œ 1 ์„ ๋บ€ ๊ฒฐ๊ณผ n-1์„ ๋ ˆ์ง€์Šคํ„ฐ์— ์ €์žฅํ•œ๋‹ค. n์ด 0 ์ด๋ผ๋ฉด 9999 ๊ฐ€ ๋Œ€์‹  ๋ ˆ์ง€์Šคํ„ฐ์— ์ €์žฅ๋œ๋‹ค.
  3. L: L ์€ n์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜๋ฅผ ์™ผํŽธ์œผ๋กœ ํšŒ์ „์‹œ์ผœ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ ˆ์ง€์Šคํ„ฐ์— ์ €์žฅํ•œ๋‹ค. ์ด ์—ฐ์‚ฐ์ด ๋๋‚˜๋ฉด ๋ ˆ์ง€์Šคํ„ฐ์— ์ €์žฅ๋œ ๋„ค ์ž๋ฆฟ์ˆ˜๋Š” ์™ผํŽธ๋ถ€ํ„ฐ d2, d3, d4, d1์ด ๋œ๋‹ค.
  4. R: R ์€ n์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜๋ฅผ ์˜ค๋ฅธํŽธ์œผ๋กœ ํšŒ์ „์‹œ์ผœ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ ˆ์ง€์Šคํ„ฐ์— ์ €์žฅํ•œ๋‹ค. ์ด ์—ฐ์‚ฐ์ด ๋๋‚˜๋ฉด ๋ ˆ์ง€์Šคํ„ฐ์— ์ €์žฅ๋œ ๋„ค ์ž๋ฆฟ์ˆ˜๋Š” ์™ผํŽธ๋ถ€ํ„ฐ d4, d1, d2, d3์ด ๋œ๋‹ค.

์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ, L ๊ณผ R ๋ช…๋ น์–ด๋Š” ์‹ญ์ง„ ์ž๋ฆฟ์ˆ˜๋ฅผ ๊ฐ€์ •ํ•˜๊ณ  ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ n = 1234 ๋ผ๋ฉด ์—ฌ๊ธฐ์— L ์„ ์ ์šฉํ•˜๋ฉด 2341 ์ด ๋˜๊ณ  R ์„ ์ ์šฉํ•˜๋ฉด 4123 ์ด ๋œ๋‹ค.

์—ฌ๋Ÿฌ๋ถ„์ด ์ž‘์„ฑํ•  ํ”„๋กœ๊ทธ๋žจ์€ ์ฃผ์–ด์ง„ ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ์ •์ˆ˜ A์™€ B(A โ‰  B)์— ๋Œ€ํ•˜์—ฌ A๋ฅผ B๋กœ ๋ฐ”๊พธ๋Š” ์ตœ์†Œํ•œ์˜ ๋ช…๋ น์–ด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ A = 1234, B = 3412 ๋ผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‘ ๊ฐœ์˜ ๋ช…๋ น์–ด๋ฅผ ์ ์šฉํ•˜๋ฉด A๋ฅผ B๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

1234 โ†’L 2341 โ†’L 3412
1234 โ†’R 4123 โ†’R 3412

๋”ฐ๋ผ์„œ ์—ฌ๋Ÿฌ๋ถ„์˜ ํ”„๋กœ๊ทธ๋žจ์€ ์ด ๊ฒฝ์šฐ์— LL ์ด๋‚˜ RR ์„ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

n์˜ ์ž๋ฆฟ์ˆ˜๋กœ 0 ์ด ํฌํ•จ๋œ ๊ฒฝ์šฐ์— ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ 1000 ์— L ์„ ์ ์šฉํ•˜๋ฉด 0001 ์ด ๋˜๋ฏ€๋กœ ๊ฒฐ๊ณผ๋Š” 1 ์ด ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ R ์„ ์ ์šฉํ•˜๋ฉด 0100 ์ด ๋˜๋ฏ€๋กœ ๊ฒฐ๊ณผ๋Š” 100 ์ด ๋œ๋‹ค.

์ž…๋ ฅ

ํ”„๋กœ๊ทธ๋žจ ์ž…๋ ฅ์€ T ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๊ฐœ์ˆ˜ T ๋Š” ์ž…๋ ฅ์˜ ์ฒซ ์ค„์— ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ๋Š” ๋‘ ๊ฐœ์˜ ์ •์ˆ˜ A์™€ B(A โ‰  B)๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๋ถ„๋ฆฌ๋˜์–ด ์ฐจ๋ก€๋กœ ์ฃผ์–ด์ง€๋Š”๋ฐ A๋Š” ๋ ˆ์ง€์Šคํ„ฐ์˜ ์ดˆ๊ธฐ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๊ณ  B๋Š” ์ตœ์ข… ๊ฐ’์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. A ์™€ B๋Š” ๋ชจ๋‘ 0 ์ด์ƒ 10,000 ๋ฏธ๋งŒ์ด๋‹ค.

์ถœ๋ ฅ

A์—์„œ B๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ตœ์†Œํ•œ์˜ ๋ช…๋ น์–ด ๋‚˜์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. ๊ฐ€๋Šฅํ•œ ๋ช…๋ น์–ด ๋‚˜์—ด์ด ์—ฌ๋Ÿฌ๊ฐ€์ง€๋ฉด, ์•„๋ฌด๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

BFS ๋ฌธ์ œ๋กœ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ํƒ์ƒ‰ํ•˜๋“ฏ์ด ํ’€๋ฉด๋œ๋‹ค.

์ „์—ฐ๋ณ€์ˆ˜์™€ ๊ฐ ํ•จ์ˆ˜๋“ค์„ ๊ตฌํ˜„ํ–ˆ๋‹ค.

int t, a, b;
bool visited[10010];

int D(int n)
{
	return (2 * n) % 10000;
}

int S(int n)
{
	int ret = n;
	ret--;
	if (ret == -1)
		ret = 9999;
	return ret;
}

int L(int n)
{
	int ret = n;
	int temp = n / 1000;
	ret = (n % 1000) * 10 + temp;
	return ret;
}

int R(int n)
{
	int temp = n % 10;
	temp *= 1000;
	return (n / 10) + temp;
}

BFS์—์„œ๋Š” ์ˆซ์ž์™€ ์ด๋•Œ๊นŒ์ง€ ์ž…๋ ฅํ•œ string์„ ๊ธฐ์–ตํ•œ๋‹ค.

void BFS()
{
	queue<pair<int, string>> q;
	q.push({ a, "" });

	while (q.empty() == false)
	{
		int num = q.front().first;
		string str = q.front().second;
		q.pop();

		if (num == b)
		{
			cout << str << '\n';
			return;
		}

		int d, s, l, r;
		d = D(num);
		l = L(num);
		r = R(num);
		s = S(num);
		if (visited[d] == false)
		{
			visited[d] = true;
			q.push({ d, str + "D" });
		}

		if (visited[s] == false)
		{
			visited[s] = true;
			q.push({ s, str + "S" });
		}

		if (visited[l] == false)
		{
			visited[l] = true;
			q.push({ l, str + "L" });
		}

		if (visited[r] == false)
		{
			visited[r] = true;
			q.push({ r, str + "R" });
		}

	}
}

visited๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•œ ์šฐ๋ฌผํŒ ํŒŒ์ง€์•Š๋„๋ก ๋งŒ๋“ค์–ด์•ผํ•œ๋‹ค.

solve ์—์„œ๋Š” BFS๋ฅผ ๋Œ๋ฆฌ๊ธฐ์ „ memset์œผ๋กœ ์ดˆ๊ธฐํ™”์‹œํ‚จ๋‹ค.

void solve()
{
	cin >> t;
	while (t--)
	{
		cin >> a >> b;
		memset(&visited, false, sizeof(visited));
		BFS();

	}
}

๐Ÿš€์ฝ”๋“œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>

using namespace std;

// DSLR

int t, a, b;
bool visited[10010];

int D(int n)
{
	return (2 * n) % 10000;
}

int S(int n)
{
	int ret = n;
	ret--;
	if (ret == -1)
		ret = 9999;
	return ret;
}

int L(int n)
{
	int ret = n;
	int temp = n / 1000;
	ret = (n % 1000) * 10 + temp;
	return ret;
}

int R(int n)
{
	int temp = n % 10;
	temp *= 1000;
	return (n / 10) + temp;
}

void BFS()
{
	queue<pair<int, string>> q;
	q.push({ a, "" });

	while (q.empty() == false)
	{
		int num = q.front().first;
		string str = q.front().second;
		q.pop();

		if (num == b)
		{
			cout << str << '\n';
			return;
		}

		int d, s, l, r;
		d = D(num);
		l = L(num);
		r = R(num);
		s = S(num);
		if (visited[d] == false)
		{
			visited[d] = true;
			q.push({ d, str + "D" });
		}

		if (visited[s] == false)
		{
			visited[s] = true;
			q.push({ s, str + "S" });
		}

		if (visited[l] == false)
		{
			visited[l] = true;
			q.push({ l, str + "L" });
		}

		if (visited[r] == false)
		{
			visited[r] = true;
			q.push({ r, str + "R" });
		}

	}
}

void solve()
{
	cin >> t;
	while (t--)
	{
		cin >> a >> b;
		memset(&visited, false, sizeof(visited));
		BFS();

	}
}

int main()
{
	FILE* stream;
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	freopen_s(&stream, "input.txt", "rt", stdin);

	solve();

	return 0;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: