๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1963_์†Œ์ˆ˜ ๊ฒฝ๋กœ

๋ฟŒ์•ผ._. 2024. 5. 7. 11:45

Gold IV

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/1963)

< ์†Œ์ˆ˜ ๊ฒฝ๋กœ >

 

๋ฌธ์ œ ํ’€์ด 

 

๋ชจ๋“  ์ž๋ฆฟ์ˆ˜์˜ ์ˆซ์ž๋ฅผ ๋ฐ”๊ฟ”๊ฐ€๋ฉฐ ์†Œ์ˆ˜์ธ์ง€ ํ™•์ธํ•œ๋‹ค. ์†Œ์ˆ˜๋ผ๋ฉด ๋‹ค์Œ ๋ณ€ํ™˜์„ ๊ณ„์†ํ•œ๋‹ค.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class _1963_ { // ์†Œ์ˆ˜ ๊ฒฝ๋กœ
	static boolean arr[];
	static int result;

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st;

		arr = new boolean[10000];

		for (int i = 2; i < 5001; i++) {
			if (!arr[i]) {
				for (int j = i + i; j < 10000; j += i) {
					arr[j] = true;
				}
			}
		}

		int t = Integer.parseInt(bf.readLine());
		for (int i = 0; i < t; i++) {
			st = new StringTokenizer(bf.readLine());
			String a = st.nextToken();
			String b = st.nextToken();

			result = -1;

			if (a.equals(b)) {
				bw.write("0\n");
			} else {
				search(a, b);

				if(result==-1) {
					bw.write("Impossible\n");
				}else {
					bw.write(result + "\n");
				}
			}
		}
		bw.flush();
	}

	private static void search(String a, String b) {
		Queue<int[]> queue = new LinkedList<>();
		queue.add(new int[] { Integer.parseInt(a), 0 });

		boolean visited[] = new boolean[10000];
		visited[Integer.parseInt(a)] = true;

		while (!queue.isEmpty()) {
			int num[] = queue.poll();

			String temp = Integer.toString(num[0]);
			for (int i = 0; i < b.length(); i++) {
				for (int j = 0; j < 10; j++) {
					if (temp.charAt(i)-'0' == j || (i==0 && j==0)) {
						continue;
					}
					String str = temp.substring(0, i) + j + temp.substring(i + 1, b.length());
					if (arr[Integer.parseInt(str)]) {
						continue;
					}
					if (str.equals(b)) {
						result = num[1] + 1;
						break;
					}
					if (!visited[Integer.parseInt(str)]) {
						visited[Integer.parseInt(str)]=true;
						queue.add(new int[] { Integer.parseInt(str), num[1] + 1 });
					}
				}
			}
			if (result != -1) {
				break;
			}
		}
	}
}
๋ณ€์ˆ˜)
arr : ์†Œ์ˆ˜ ํŒ๋‹จ
t : ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜
a, b : ๋„ค ์ž๋ฆฌ ์†Œ์ˆ˜ 
result : ๋‘ ์†Œ์ˆ˜ ์‚ฌ์ด์˜ ๋ณ€ํ™˜์— ํ•„์š”ํ•œ ์ตœ์†Œ ํšŒ์ˆ˜

 

๋„ค ์ž๋ฆฌ ์ˆ˜๋งŒ ์†Œ์ˆ˜์ธ์ง€ ํŒ๋‹จํ•˜๋ฏ€๋กœ 9999๊นŒ์ง€ ๋ฏธ๋ฆฌ ์†Œ์ˆ˜๋ฅผ ํŒ๋‹จํ•˜์—ฌ arr์— boolean ๊ฐ’์œผ๋กœ ์ €์žฅํ•œ๋‹ค. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜ t๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ํ›„ t๋งŒํผ ๋„ค ์ž๋ฆฌ ์†Œ์ˆ˜ 1์Œ์„ ์ž…๋ ฅ๋ฐ›์•„ a, b์— ์ €์žฅํ•œ๋‹ค. ์ž…๋ ฅ๋ฐ›์€ a, b๊ฐ€ ๊ฐ™์€ ๊ฐ’์ด๋ผ๋ฉด ๊ณ„์‚ฐํ•˜์ง€ ์•Š๊ณ  0์„ ์ถœ๋ ฅํ•œ๋‹ค. ๊ฐ’์ด ๋‹ค๋ฅด๋‹ค๋ฉด search ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ๋งŒ์•ฝ ๋ณ€ํ™˜์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด "Impossible"์„ ์ถœ๋ ฅํ•˜๊ณ , ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด result ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

search ( String a, String b)

Queue์— [ํ˜„์žฌ ๊ฐ’, ๋ณ€ํ™˜ ํšŸ์ˆ˜]๋ฅผ ๋ฐฐ์—ด๋กœ ์ €์žฅํ•œ๋‹ค. ์ดˆ๊ธฐ ๊ฐ’์ธ a๋ฅผ ๋ฐฉ๋ฌธ ํ‘œ์‹œ ํ›„ queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋‹ค์Œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

1) queue poll

2) ๊ฐ’์„ ๋งจ ์•ž์ž๋ฆฌ๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ 0~9๋กœ ๋ฐ”๊พธ๊ธฐ

3) ๋งŒ์•ฝ ๋งจ ์•ž์ž๋ฆฌ๊ฐ€ 0์ด๊ฑฐ๋‚˜ ๋ฐ”๊พธ๋ ค๋Š” ๊ฐ’๊ณผ ์ง€๊ธˆ ๊ฐ’์ด ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ๋„˜์–ด๊ฐ„๋‹ค

4) ๋ฐ”๊พผ ๊ฐ’์ด ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ๋„˜์–ด๊ฐ„๋‹ค

5) ๋ฐ”๊พผ ๊ฐ’์ด b์™€ ์ผ์น˜ํ•˜๋‹ค๋ฉด result ์—…๋ฐ์ดํŠธ ํ›„ ์ข…๋ฃŒ 

6) ๋ฐ”๊พผ ๊ฐ’์ด ์†Œ์ˆ˜์ด๊ณ , b์™€ ์ผ์น˜ํ•˜์ง€ ์•Š๊ณ , ์•„์ง ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ ํ›„ queue์— ์ถ”๊ฐ€