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

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

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

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

 

12851๋ฒˆ: ์ˆจ๋ฐ”๊ผญ์งˆ 2

์ˆ˜๋นˆ์ด๋Š” ๋™์ƒ๊ณผ ์ˆจ๋ฐ”๊ผญ์งˆ์„ ํ•˜๊ณ  ์žˆ๋‹ค. ์ˆ˜๋นˆ์ด๋Š” ํ˜„์žฌ ์  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.StringTokenizer;

public class BOJ_12851 {
	static Deque<int[]> que;
	static int[] visited = new int[100_001];
	static int n, k, cnt;
	
	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 ์ตœ์†Ÿ๊ฐ’ ๊ฐฑ์‹ 
					if(visited[k] > sec+1) {
						visited[k] = sec+1;
						cnt = 1;
					} else if(visited[k] == sec+1) {
						cnt++;
					}
					continue;
				}
				if(cal[i] < 0 || cal[i] >= visited.length || visited[cal[i]] <= sec) continue;
				
				que.add(new int[]{cal[i], sec+1});
				visited[cal[i]] = sec+1;
			}
		}
		return visited[k];
	}
	
	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		
		st = new StringTokenizer(in.readLine());
		n = Integer.parseInt(st.nextToken());
		k = Integer.parseInt(st.nextToken());;
		
		if(n == k) System.out.println(0 + "\n" + 1);
		else {
			for(int i = 0; i < visited.length; i++) visited[i] = Integer.MAX_VALUE;
			que = new ArrayDeque<>();
			que.add(new int[]{n, 0});
			
			System.out.println(bfs() + "\n" + cnt);
		}		
	}

}

๐Ÿ‘‰ BFS ๋ฌธ์ œ

โœ”๏ธ #13549 ์ˆจ๋ฐ”๊ผญ์งˆ3 ๋ฌธ์ œ์™€ ๋งค์šฐ ์œ ์‚ฌ!

 

  • visited ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ๋งค ์ž๋ฆฌ์˜ ์ตœ์†Œ ํƒ์ƒ‰ ์‹œ๊ฐ„์„ ์—…๋ฐ์ดํŠธํ•ด์ค€๋‹ค๋Š” ์ ์€ ์ˆจ๋ฐ”๊ผญ์งˆ3 ๋ฌธ์ œ์™€ ๊ฐ™๋‹ค.
  • ์ด ๋ฌธ์ œ์˜ ํฌ์ธํŠธ๋Š” ๋™์ƒ์„ ๋งŒ๋‚ฌ์„ ๋•Œ!!
    ๋™์ƒ์„ ๋งŒ๋‚ฌ์„ ๋•Œ, visited ๋ฐฐ์—ด์˜ ๋™์ƒ ์ž๋ฆฌ์ธ visited[k] ์ž๋ฆฌ์— ์ €์žฅ๋˜์–ด์žˆ๋Š” ๊ฐ’์ด
    ํ˜„์žฌ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒฝ๋กœ์˜ ์‹œ๊ฐ„๋ณด๋‹ค ๋” ํฌ๋‹ค๋ฉด, ๋‹น์—ฐํžˆ visited[k]์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ฐฑ์‹ ํ•ด์ค˜์•ผํ•œ๋‹ค.
    ๊ทธ๋Ÿฐ๋ฐ ์ด ๋•Œ visited[k]๋งŒ ๊ฐฑ์‹ ์‹œ์ผœ์ค„ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, cnt๋„ ํ•จ๊ป˜ ์ดˆ๊ธฐํ™”ํ•ด์ค˜์•ผ ํ•œ๋‹ค.
  • ๋™์ƒ์„ ์ฐพ๋Š”๋ฐ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„์˜ ์ตœ์†Ÿ๊ฐ’์ด ๋ณ€๊ฒฝ๋œ๋‹ค๋Š” ๊ฒƒ์€
    ๊ทธ ์ „๊นŒ์ง€ ๋ˆ„์ ํ–ˆ๋˜ ํ•ด๋‹น ์ตœ์†Œ ํƒ์ƒ‰ ์‹œ๊ฐ„์— ๋Œ€ํ•œ ์นด์šดํŒ…๋„ ์ƒˆ๋กญ๊ฒŒ ํ•ด์•ผํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๊ธฐ ๋•Œ๋ฌธ!
  • ๊ทธ๋ฆฌ๊ณ  ๋งŒ์•ฝ visited[k] ๊ฐ’์ด ํ˜„์žฌ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒฝ๋กœ์˜ ์‹œ๊ฐ„๊ณผ ๊ฐ™๋‹ค๋ฉด ๊ทธ๋Œ€๋กœ cnt๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ ์นด์šดํŒ…ํ–ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ๋ชจ๋“  ์ž๋ฆฌ๋งˆ๋‹ค, ์‹œ๊ฐ„์€ ๊ฐ™์•„๋„ ๊ฒฝ๋กœ๋Š” ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์„ ์บ์น˜ํ•ด์ฃผ๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

๋Œ“๊ธ€