๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2๏ธโƒฃ Java/Problem Solving

[Java Algorithm] ์ˆจ๋ฐ”๊ผญ์งˆ 4 BOJ #13913

by seolhee2750 2022. 8. 8.
๋ฌธ์ œ

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

 

13913๋ฒˆ: ์ˆจ๋ฐ”๊ผญ์งˆ 4

์ˆ˜๋นˆ์ด๋Š” ๋™์ƒ๊ณผ ์ˆจ๋ฐ”๊ผญ์งˆ์„ ํ•˜๊ณ  ์žˆ๋‹ค. ์ˆ˜๋นˆ์ด๋Š” ํ˜„์žฌ ์  N(0 ≤ N ≤ 100,000)์— ์žˆ๊ณ , ๋™์ƒ์€ ์  K(0 ≤ K ≤ 100,000)์— ์žˆ๋‹ค. ์ˆ˜๋นˆ์ด๋Š” ๊ฑท๊ฑฐ๋‚˜ ์ˆœ๊ฐ„์ด๋™์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ, ์ˆ˜๋นˆ์ด์˜ ์œ„์น˜๊ฐ€ X์ผ

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;
import java.util.StringTokenizer;

public class Main {
	
	static Deque<int[]> que;
	static boolean[] visited = new boolean[100_001];
	static int[] parent = new int[100_001];
	static int n, k;
	
	public static int[] bfs() {
		while(!que.isEmpty()) {
			int[] now = que.removeFirst();
			int x = now[0];
			int sec = now[1];
			int[] cal = {x+1, x-1, x*2};
			
			for(int i = 0; i < 3; i++) {
				if(cal[i] == k) {
					visited[cal[i]] = true;
					parent[cal[i]] = x;
					return new int[]{cal[i], sec+1};
				}
				if(cal[i] < 0 || cal[i] >= visited.length || visited[cal[i]]) continue;
				
				que.add(new int[]{cal[i], sec+1});
				visited[cal[i]] = true; 
				parent[cal[i]] = x; // ์ด์ „์— ์–ด๋””์„œ ์ด์–ด์ง„๊ฑด์ง€ ๊ธฐ๋กํ•ด๋‘๊ธฐ
			}
		}
		return new int[]{};
	}
	
	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		StringBuilder out = new StringBuilder();
		
		st = new StringTokenizer(in.readLine());
		n = Integer.parseInt(st.nextToken());
		k = Integer.parseInt(st.nextToken());
		
		if(n == k) {
			System.out.println(0 + "\n" + n);
		}
		else {
			visited[n] = true;
			parent[n] = n;
			que = new ArrayDeque<>();
			que.add(new int[]{n, 0});
			
			// ๊ฑฐ๊พธ๋กœ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์ด์–ด์ง„ ๋…ธ๋“œ๋“ค stack์— ๋„ฃ์—ˆ๋‹ค๊ฐ€ ๋‹ค์‹œ ๋นผ๋ฉด์„œ ์ถœ๋ ฅ
			Stack<Integer> stack = new Stack<>();
			int[] result = bfs();
			int idx = result[0];
			while(idx != n) {
				stack.push(parent[idx]);
				idx = parent[idx];
			}
			
			out.append(result[1] + "\n");
			while(!stack.isEmpty()) {
				out.append(stack.pop() + " ");
			}
			System.out.println(out.append(result[0]));
		}
	}

}

๐Ÿ‘‰ BFS ๋ฌธ์ œ
์ด ๋ฌธ์ œ๋Š” ๋ฐœ์ƒ ๊ฐ€๋Šฅํ•œ ์‹œ๊ฐ„ ์ดˆ๊ณผ, ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ, ์˜ˆ์™ธ๋“ค์„ ์ž˜ ์ƒ๊ฐํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š” ๊ฒƒ ๊ฐ™์Œ

  • ์‹œ์ž‘์ ๊ณผ ๋„์ฐฉ์ ์ด ๊ฐ™์„ ๊ฒฝ์šฐ ๋”ฐ๋กœ ๋นผ์„œ ์ฒ˜๋ฆฌํ•ด์ฃผ์—ˆ๋‹ค.
  • visited๋Š” ๋‹จ์ˆœํžˆ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ๋ฐฐ์—ด์ด๋ฉฐ,
    parent๋Š” ํ˜„์žฌ ์ธ๋ฑ์Šค์— ์˜ค๊ธฐ ์ „์— ๋ฐฉ๋ฌธํ•œ ์ธ๋ฑ์Šค๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด์ด๋‹ค.
    (์›๋ž˜๋Š” ์ด ๋‘ ๋ฐฐ์—ด์„ ํ•˜๋‚˜๋กœ ๋ฌถ๊ณ , intํ˜•์œผ๋กœ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ–ˆ์ง€๋งŒ
    ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ์‹œ์ž‘์ ์ด 0์ผ ๊ฒฝ์šฐ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.)
  • bfs ๋ฉ”์„œ๋“œ๋Š” int[]ํ˜•์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•˜์—ฌ์„œ {๋„์ฐฉ ์ธ๋ฑ์Šค, ๊ฑธ๋ฆฐ ์‹œ๊ฐ„}์„ ํ•จ๊ป˜ ๊ฐ€์ ธ์™”๋‹ค.
  • bfs ๋ฉ”์„œ๋“œ์˜ ๊ฒฝ์šฐ ์•„์ฃผ ๊ฐ„๋‹จํ•œ๋ฐ, ํ•˜๋‚˜ ์ฒดํฌํ•  ์ ์€
    parent์— ์ด์ „์— ๋“ค๋ฆฐ ์ธ๋ฑ์Šค ์ •๋ณด๋ฅผ ๊ผญ ๋„ฃ์–ด์ค„ ๊ฒƒ,,!!
  • bfs ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฐ˜ํ™˜๋˜๋ฉด ์Šคํƒ์— ๋„์ฐฉ์ง€์ ๋ถ€ํ„ฐ ๊ฑฐ๊พธ๋กœ ๊ฒฝ๋กœ๋ฅผ ๋„ฃ์–ด์ค€ ํ›„,
    ๋‹ค์‹œ popํ•˜๋ฉด์„œ ์ฒ˜์Œ ์‹œ์ž‘ ์ง€์ ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ–ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์ˆจ๋ฐ”๊ผญ์งˆ3 ๋ฌธ์ œ์™€ ๋งค์šฐ ์œ ์‚ฌํ•˜์ง€๋งŒ, ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์˜ˆ์™ธ๋ฅผ ์ž˜ ์ฒดํฌํ•ด์ค˜์•ผ ํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.
  • ์ฒ˜์Œ์—” ์‰ฝ๋„ค~~ ํ–ˆ๋‹ค๊ฐ€,, ์ •๋‹ต๋ฅ  ๋‚ฎ์€ ์ด์œ ๋ฅผ ์•Œ์•„๋ฒ„๋ ธ๋‹ค.ใ…Žใ…Ž

๋Œ“๊ธ€