3 ๋ถ„ ์†Œ์š”

๐Ÿ“ [G4_9019] DSLR

  • ์‹œ๊ฐ„์ดˆ๊ณผ..
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {

	// ์ˆซ์ž, ํ•จ์ˆ˜์ด๋ฆ„(D,S,L,R)
	static class ch {
		int num;
		String ch;

		public ch(int num, String ch) {
			this.num = num;
			this.ch = ch;
		}

	}

	static int A, B;
	static Queue<ch> queue;
	static String res;

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		StringTokenizer st;

		// **** input start ****
		int T = Integer.parseInt(br.readLine());

		for (int tc = 0; tc < T; tc++) {
			st = new StringTokenizer(br.readLine(), " ");
			A = Integer.parseInt(st.nextToken());
			B = Integer.parseInt(st.nextToken());

		// **** input end ****

		String[] DSLR = { "D", "S", "L", "R" };
		int min = Integer.MAX_VALUE;
		String output = "";
		// 4 ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ์–ด๋ณด๊ธฐ
		for (int i = 0; i < 4; i++) {
			res = "";
			queue = new LinkedList<ch>();
			queue.add(new ch(A, DSLR[i]));
			BFS();

			min = Math.min(min, res.length());
			if(min == res.length()) {
				output = res;
			}
		}
		sb.append(output).append("\n");
		}
		System.out.println(sb);
	} // main end

	// BFS
	static void BFS() {
		while (!queue.isEmpty()) {
			ch c = queue.poll();
			int temp = c.num;
			String str = c.ch;

			// B๋กœ ๋˜๋ฉด
			if (temp == B) {
				str = str.substring(1, str.length());
				res += str;
				break;
			}

			// DSLR ์ˆœ์„œ๋Œ€๋กœ

			// D
			queue.add(new ch(D(temp), str + "D"));

			// S
			queue.add(new ch(S(temp), str + "S"));

			// L
			queue.add(new ch(L(temp), str + "L"));

			// R
			queue.add(new ch(R(temp), str + "R"));

		}

	}

	// D = 2*N
	static int D(int num) {
		num *= 2;

		// ๋งŒ์•ฝ ๊ณฑํ•˜๊ธฐ 2ํ•œ ๊ฐ’์ด 9999๋ณด๋‹ค ํด๊ฒฝ์šฐ mod 10000 ํ•ด์ฃผ๊ธฐ
		if (num > 9999)
			num %= 10000;
		return num;
	}

	// S = N-1
	static int S(int num) {
		num -= 1;

		// 1 ๋บ€ ๊ฐ’์ด 0์ผ ๊ฒฝ์šฐ 9999 ์ €์žฅ
		if (num == 0)
			num = 9999;
		return num;
	}

	// L = left
	static int L(int num) {
		int temp = num;

		int thousand = (temp / 1000);
		temp %= 1000;
		int hundred = (temp / 100);
		temp %= 100;
		int ten = (temp / 10);
		temp %= 10;
		int one = temp;

		num = hundred * 1000 + ten * 100 + one * 10 + thousand;
		return num;
	}

	// R = right
	static int R(int num) {
		int temp = num;

		int thousand = (temp / 1000);
		temp %= 1000;
		int hundred = (temp / 100);
		temp %= 100;
		int ten = (temp / 10);
		temp %= 10;
		int one = temp;

		num = one * 1000 + thousand * 100 + hundred * 10 + ten;
		return num;
	}
} // class end
  • ์ฐพ์€ ๋ฐฉ๋ฒ•..
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main{
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		StringTokenizer st = null;

		// **** input start ****
		int T = Integer.parseInt(br.readLine());

		for (int tc = 0; tc < T; tc++) {
			st = new StringTokenizer(br.readLine(), " ");
			int A = Integer.parseInt(st.nextToken());
			int B = Integer.parseInt(st.nextToken());
			// **** input end ****

			boolean[] v = new boolean[10000];
			String[] command = new String[10000];
			Queue<Integer> queue = new LinkedList<Integer>();

			queue.add(A);
			// ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
			v[A] = true;
			Arrays.fill(command, "");

			while (!queue.isEmpty() && !v[B]) {
				int temp = queue.poll();

				// DSLR
				int D = (temp * 2) % 10000;
				int S = temp == 0 ? 9999 : temp - 1;
				int L = (temp % 1000) * 10 + (temp / 1000);
				int R = (temp % 10) * 1000 + (temp / 10);

				if (!v[D]) {
					// ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
					v[D] = true;
					// ๋ฌธ์ž ์ถ”๊ฐ€
					command[D] = command[temp] + "D";
					// queue ๋”ํ•˜๊ธฐ
					queue.add(D);
				}
				if (!v[S]) {
					// ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
					v[S] = true;
					// ๋ฌธ์ž ์ถ”๊ฐ€
					command[S] = command[temp] + "S";
					// queue ๋”ํ•˜๊ธฐ
					queue.add(S);
				}
				if (!v[L]) {
					// ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
					v[L] = true;
					// ๋ฌธ์ž ์ถ”๊ฐ€
					command[L] = command[temp] + "L";
					// queue ๋”ํ•˜๊ธฐ
					queue.add(L);
				}
				if (!v[R]) {
					// ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
					v[R] = true;
					// ๋ฌธ์ž ์ถ”๊ฐ€
					command[R] = command[temp] + "R";
					// queue ๋”ํ•˜๊ธฐ
					queue.add(R);
				}
			}
			sb.append(command[B]).append("\n");
		}
		System.out.println(sb);
	} // main end
} // class end

๐Ÿค” ๋‚˜์˜ ์ƒ๊ฐ

์ฒ˜์Œ์— BFS () + DSLR ์„ ์ด์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ .. ์‹œ๊ฐ„์ดˆ๊ณผ..
๊ทธ๋ž˜์„œ ์ฐพ์•„๋ณด๋‹ˆ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๊ฐ€ ์‹œ๊ฐ„์ดˆ๊ณผ์˜ ์›์ธ์ด์˜€๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๋‚˜๋Š” ์ˆ˜์™€ ๋ฌธ์ž๋ฅผ class๋กœ ๋”ฐ๋กœ ๋‘ฌ์„œ ๋‘˜ ๋‹ค ๊ณ ๋ คํ•ด์คฌ๋Š”๋ฐ ๊ทธ๋Ÿด ํ•„์š” ์—†์ด ์ˆ˜๋กœ๋งŒ ์ฒดํฌํ•˜๊ณ  ๋ฌธ์ž๋Š” ๋ฐฐ์—ด์„ ์ด์šฉํ•ด ์ตœ๋Œ€ 10000์ด๋‹ˆ 10000๊ฐœ๋ฅผ ๊ฐ€์ง„ ๋ฐฐ์—ด์—์„œ ๋ฌธ์ž๋ฅผ ์ €์žฅํ•ด์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด์˜€๋‹ค.
์ด๊ฑด ์ƒ๊ฐ์ง€๋„ ๋ชปํ•œ..
๊ทธ๋ฆฌ๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด ํ™•์ธ.
๋˜ํ•œ โ€˜Lโ€™,โ€™Rโ€™์„ ๊ตฌํ• ๋•Œ ์–ด์ฐจํ”ผ ๊ทœ์น™์ ์œผ๋กœ ์ด๋™ํ•˜๋‹ˆ ๊ณฑํ•˜๊ณ  ๋‚˜๋ˆ„๊ธฐ๋งŒ ํ•ด์ฃผ๋ฉด ๋” ๊ฐ„๋‹จํ–ˆ๋‹ค.
๊ทธ๋ž˜๋„ BFS๋กœ ์ ‘๊ทผํ•œ ๊ฒƒ์ด ์•„์ฃผ ๋งŒ์กฑ์Šค.. ์™œ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ๊นŒ๋จน์—ˆ์„๊นŒ..
์•„์ˆฉ๋‹ค..

ํƒœ๊ทธ: , , ,

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

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