๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ

[BOJ] 1697 ์ˆจ๋ฐ”๊ผญ์งˆ [Java]

category Algorithm/BOJ 2022. 8. 25. 19:26

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

 

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

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

www.acmicpc.net


1. ๋ฌธ์ œ ์š”์•ฝ

์  N๊ณผ ์  K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. N์—์„œ K๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ตœ๋‹จ์‹œ๊ฐ„์„ ์ฐพ๋Š” ๋ฌธ์ œ์ด๋‹ค.

์  N์€ 1์ดˆ๋งˆ๋‹ค +1, -1, *2 ์œ„์น˜๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.

(0 ≤ N, K ≤ 100,000)

 

2. ํ’€์ด

์ด ๋ฌธ์ œ๋Š” BFS์˜ ๊นŠ์ด๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

์˜ˆ์ œ 5 17์„ ๊ธฐ์ค€์œผ๋กœ, ์ฒ˜์Œ์—๋Š” '๊นŠ์ด'๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌํ• ๊นŒ ์ƒ๊ฐํ•˜๋ฉด์„œ 5์—์„œ 17๊นŒ์ง€ ๊ฐ€๋Š” ๋งŽ์€ ๊ฒฝ๋กœ ์ค‘ ๊ฐ€์žฅ ์งง์€ ๊นŠ์ด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ๋ฐฉ๋ฒ•์€ ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ํƒ์ƒ‰ ํ›„ ๋น„๊ตํ•ด์•ผํ•˜๋ฏ€๋กœ ๋น„ํšจ์œจ์ ์ด์—ˆ๋‹ค. 

๋‹ค์Œ์œผ๋กœ ์ƒ๊ฐํ•œ ๋ฐฉ๋ฒ•์€ BFS๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ํ์— ๊นŠ์ด๋„ ๊ฐ™์ด ์ €์žฅํ•˜์˜€๋‹ค.

17์ด ์žˆ๋Š” ๊นŠ์ด๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ๊ณ  returnํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ด ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

'ํ† ๋งˆํ† ' ๋ฌธ์ œ์™€ ๋น„์Šทํ•œ ๋Š๋‚Œ์ด์—ˆ๋‹ค. ์ต๋Š” ๋‚ ์งœ๋ฅผ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด ๊นŠ์ด๋ฅผ ํ์— ๊ฐ™์ด ์ €์žฅํ–ˆ๋‹ค.

์ผ์ˆ˜, ์‹œ๊ฐ„ ๋“ฑ๋“ฑ์„ ์ฒดํฌํ•ด์ค„ ๋•Œ๋Š” ํ์— ๊นŠ์ด๋„ ๊ฐ™์ด ๋„˜๊ฒจ์ฃผ์ž. 

 

์ „ํ˜•์ ์ธ BFS ํ‹€์„ ์‚ฌ์šฉํ•˜์˜€๊ณ , ๋ธํƒ€๋ฅผ ๋งŒ๋“ค์–ด ๋‹ค์Œ ์œ„์น˜๊ฐ€ +1, -1, *2 ์ธ ๊ฐ๊ฐ์˜ ์ขŒํ‘œ๋ฅผ ํ์— ๋„ฃ์–ด์คฌ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์ œ์ถœํ•˜๋ฉด ๊ณ„์† ํ‹€๋ ธ๋‹ค๊ณ  ๋‚˜์™”๋‹ค. ๋‚ด๊ฐ€ ๋†“์นœ ๋ถ€๋ถ„์€ 2๊ฐ€์ง€์˜€๋‹ค.

1. N์ด K๋ณด๋‹ค ํฐ ์œ„์น˜์— ์žˆ๋Š” ๊ฒฝ์šฐ

- N > K์ผ ๋•Œ K๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ -1์„ ์“ฐ๋Š” ๊ฒƒ ๋ฐ–์— ์—†๋‹ค. ๋”ฐ๋ผ์„œ ์ด ๊ฒฝ์šฐ์—๋Š” ์กฐ๊ฑด์„ ๋”ฐ๋กœ ์ค˜์•ผ ํ•œ๋‹ค.

- N > K ๋ผ๋ฉด, N - K๋ฒˆ -1ํ•ด์ฃผ๋ฉด ๋˜๋ฏ€๋กœ N-K๋ฅผ ์ถœ๋ ฅํ•ด์คฌ๋‹ค.

2. ๊นŠ์ด๊ฐ€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์ง€๋งŒ ์‹œ๊ฐ„์€ 1์ดˆ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ๊นŠ์ด + 1์„ ํ•ด์ค˜์•ผ ํ–ˆ๋Š”๋ฐ, ์ด๊ฒƒ์„ main์—์„œ ๊ณตํ†ต์œผ๋กœ ์ถœ๋ ฅํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ํ•ด์ค€๊ฒŒ ๋ฌธ์ œ์˜€๋‹ค. BFS ๋‚ด์—์„œ return ํ•˜๊ธฐ ๋ฐ”๋กœ ์ „ ๊นŠ์ด๋ฅผ ๋ณ€์ˆ˜์— ๋‹ด๊ณ  +1์„ ํ•ด์ค˜์„œ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

-> ๋ธํƒ€๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๊นŠ์ด๋ฅผ ํ์— ๊ฐ™์ด ์ €์žฅํ•˜๋ฉด์„œ BFS๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

 

3. ๋ฌธ์ œ์ 

 

 

4. ์ฝ”๋“œ

๋”๋ณด๊ธฐ
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;

public class BOJ_1697_์ˆจ๋ฐ”๊ผญ์งˆ2 {

	static int[] dir = {1, -1, 2};
	static int[] map;
	static int N, sister, ans, min;
	static boolean[] visited;
	static Queue<Node> queue = new ArrayDeque<> ();
	
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		N = Integer.parseInt(st.nextToken());
		sister = Integer.parseInt(st.nextToken());
		map = new int[100_000+1];
		visited = new boolean[100_000+1];
		
		
		if (N >= sister) ans = N - sister; //N >= sister ๊ฒฝ์šฐ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ -1๋ฐ–์— ์—†๋‹ค
		else bfs(N);
		System.out.println(ans);
		
	}
	
	static void bfs(int start) {
		queue.add(new Node(start, 0));
		visited[start] = true;
		while (!queue.isEmpty()) {
			Node node = queue.poll();
			int next = 0;
			for (int d = 0; d < 3; d++) { //๋ธํƒ€ ์‚ฌ์šฉ
				if (d == 2)  next = node.x * 2;
				else next = node.x + dir[d];
				if (next < 0 || next > 100000 || visited[next]) continue;
				if (next == sister) {
					ans = node.depth + 1; //์—ฌ๊ธฐ์‹œ +1 (์ดˆ๋Š” 1์ดˆ๋ถ€ํ„ฐ, ๊นŠ์ด๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ)
					return;
				}
				visited[next] = true;
				queue.add(new Node(next, node.depth+1)); //ํ์— ๊นŠ์ด+1์„ ์ €์žฅ
			}
		}
	}
	
	static class Node {
		int x, depth;
		public Node(int x, int depth) { //์‹œ๊ฐ„ == ๊นŠ์ด
			this.x = x;
			this.depth = depth;
		}
	}
}