Post

🧩 Baekjoon 1011 - Fly me to the Alpha Centauri

🧩 Baekjoon 1011 - Fly me to the Alpha Centauri

문제

μš°ν˜„μ΄λŠ” μ–΄λ¦° μ‹œμ ˆ, 지ꡬ μ™Έμ˜ λ‹€λ₯Έ ν–‰μ„±μ—μ„œλ„ 인λ₯˜λ“€μ΄ μ‚΄μ•„κ°ˆ 수 μžˆλŠ” λ―Έλž˜κ°€ 였리라 λ―Ώμ—ˆλ‹€. 그리고 κ·Έκ°€ μ§€κ΅¬λΌλŠ” 세상에 λ°œμ„ λ‚΄λ € 놓은 μ§€ 23년이 μ§€λ‚œ μ§€κΈˆ, 세계 μ΅œμ—°μ†Œ ASNA 우주 비행사가 λ˜μ–΄ μƒˆλ‘œμš΄ 세계에 λ°œμ„ λ‚΄λ € λ†“λŠ” μ˜κ΄‘μ˜ μˆœκ°„μ„ 기닀리고 μžˆλ‹€. κ·Έκ°€ νƒ‘μŠΉν•˜κ²Œ 될 μš°μ£Όμ„ μ€ Alpha CentauriλΌλŠ” μƒˆλ‘œμš΄ 인λ₯˜μ˜ 보금자리λ₯Ό κ°œμ²™ν•˜κΈ° μœ„ν•œ λŒ€κ·œλͺ¨ μƒν™œ μœ μ§€ μ‹œμŠ€ν…œμ„ νƒ‘μž¬ν•˜κ³  있기 λ•Œλ¬Έμ—, κ·Έ 크기와 μ§ˆλŸ‰μ΄ μ—„μ²­λ‚œ 이유둜 μ΅œμ‹ κΈ°μˆ λ ₯을 총 λ™μ›ν•˜μ—¬ κ°œλ°œν•œ 곡간이동 μž₯치λ₯Ό νƒ‘μž¬ν•˜μ˜€λ‹€. ν•˜μ§€λ§Œ 이 곡간이동 μž₯μΉ˜λŠ” 이동 거리λ₯Ό κΈ‰κ²©ν•˜κ²Œ 늘릴 경우 기계에 μ‹¬κ°ν•œ 결함이 λ°œμƒν•˜λŠ” 단점이 μžˆμ–΄μ„œ, 이전 μž‘λ™μ‹œκΈ°μ— k광년을 μ΄λ™ν•˜μ˜€μ„ λ•ŒλŠ” k-1 , k ν˜Ήμ€ k+1 κ΄‘λ…„λ§Œμ„ λ‹€μ‹œ 이동할 수 μžˆλ‹€. 예λ₯Ό λ“€μ–΄, 이 μž₯치λ₯Ό 처음 μž‘λ™μ‹œν‚¬ 경우 -1 , 0 , 1 광년을 이둠상 이동할 수 μžˆμœΌλ‚˜ 사싀상 음수 ν˜Ήμ€ 0 거리만큼의 이동은 μ˜λ―Έκ°€ μ—†μœΌλ―€λ‘œ 1 광년을 이동할 수 있으며, κ·Έ λ‹€μŒμ—λŠ” 0 , 1 , 2 광년을 이동할 수 μžˆλŠ” 것이닀. ( μ—¬κΈ°μ„œ λ‹€μ‹œ 2광년을 μ΄λ™ν•œλ‹€λ©΄ λ‹€μŒ μ‹œκΈ°μ—” 1, 2, 3 광년을 이동할 수 μžˆλ‹€. ) κΉ€μš°ν˜„μ€ 곡간이동 μž₯치 μž‘λ™μ‹œμ˜ μ—λ„ˆμ§€ μ†Œλͺ¨κ°€ ν¬λ‹€λŠ” 점을 잘 μ•Œκ³  있기 λ•Œλ¬Έμ— xμ§€μ μ—μ„œ y지점을 ν–₯ν•΄ μ΅œμ†Œν•œμ˜ μž‘λ™ 횟수둜 μ΄λ™ν•˜λ € ν•œλ‹€. ν•˜μ§€λ§Œ y지점에 λ„μ°©ν•΄μ„œλ„ 곡간 이동μž₯치의 μ•ˆμ „μ„±μ„ μœ„ν•˜μ—¬ y지점에 λ„μ°©ν•˜κΈ° λ°”λ‘œ μ§μ „μ˜ μ΄λ™κ±°λ¦¬λŠ” λ°˜λ“œμ‹œ 1κ΄‘λ…„μœΌλ‘œ ν•˜λ € ν•œλ‹€. κΉ€μš°ν˜„μ„ μœ„ν•΄ x지점뢀터 μ •ν™•νžˆ yμ§€μ μœΌλ‘œ μ΄λ™ν•˜λŠ”λ° ν•„μš”ν•œ 곡간 이동 μž₯치 μž‘λ™ 횟수의 μ΅œμ†Ÿκ°’μ„ κ΅¬ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λΌ.

μž…λ ₯

μž…λ ₯의 첫 μ€„μ—λŠ” ν…ŒμŠ€νŠΈμΌ€μ΄μŠ€μ˜ 개수 Tκ°€ μ£Όμ–΄μ§„λ‹€. 각각의 ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€μ— λŒ€ν•΄ ν˜„μž¬ μœ„μΉ˜ x 와 λͺ©ν‘œ μœ„μΉ˜ y κ°€ μ •μˆ˜λ‘œ μ£Όμ–΄μ§€λ©°, xλŠ” 항상 y보닀 μž‘μ€ 값을 κ°–λŠ”λ‹€. (0 ≀ x < y < 2^31)

좜λ ₯

κ°Β ν…ŒμŠ€νŠΈΒ μΌ€μ΄μŠ€μ—Β λŒ€ν•΄Β xμ§€μ μœΌλ‘œλΆ€ν„°Β yμ§€μ κΉŒμ§€Β μ •ν™•νžˆΒ λ„λ‹¬ν•˜λŠ”λ°Β ν•„μš”ν•œΒ μ΅œμ†Œν•œμ˜Β κ³΅κ°„μ΄λ™Β μž₯μΉ˜Β μž‘λ™Β νšŸμˆ˜λ₯ΌΒ μΆœλ ₯ν•œλ‹€.

예제

βœ… μž…λ ₯ 1

1
2
3
4
3 
0 3 
1 5 
45 50

βœ… 좜λ ₯ 1

1
2
3
3 
3 
4

μž‘μ„± μ½”λ“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		
		for (int i = 0; i < t; i++) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			int x = Integer.parseInt(st.nextToken());
			int y = Integer.parseInt(st.nextToken());
			int count = 0;
			
			if (x == y) {
				System.out.println(count);
				continue;
			}
			
			bfs(x, y);
		}
	}
	
	private static void bfs(int x, int y) {
		int distance = y - x;
		boolean[] visited = new boolean[distance + 1];
		Deque<int[]> queue = new ArrayDeque<>();
		queue.addLast(new int[]{x, 0, 0});
		visited[0] = true;
		
		while (!queue.isEmpty()) {
			int[] element = queue.pollFirst();
			int prev = element[0];
			int count = element[1];
			int move = element[2];
			
			if (prev == y - 1) {
				System.out.println(count + 1);
				return;
			}
			
			for (int d = move - 1; d <= move + 1; d++) {
				if (d <= 0) continue;
				
				int current = prev + d;
				
				if (current < y && !visited[current - x]) {
					queue.addLast(new int[]{current, count + 1, d});
					visited[current - x] = true;
				}
			}
		}
	}
}
  • BFS둜 문제λ₯Ό ν’€μ—ˆμœΌλ‚˜, λ©”λͺ¨λ¦¬ 초과 λ¬Έμ œκ°€ λ°œμƒν–ˆλ‹€.
  • ν’€κ³  λ‚˜λ‹ˆ 문제 μžμ²΄κ°€ λ©”λͺ¨λ¦¬ μΈ‘λ©΄μ—μ„œ BFS에 μ ν•©ν•˜μ§€ μ•Šλ‹€λŠ” κ±Έ μ•Œκ²Œ λ˜μ—ˆλ‹€.

κ°œμ„  μ½”λ“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Β Β Β Β 1
Β Β Β Β 11
Β Β Β Β 111
Β Β Β Β 121
Β Β Β Β 1211
Β Β Β Β 1121
Β Β Β Β 1221
Β Β Β Β 12211
Β Β Β Β 11221
Β Β Β Β 12221
Β Β Β Β ...
*/

public class Main {
	public static void main(String[] args) throws IOException {
		// 1. λ³€μˆ˜ μ„ μ–Έ 및 μ΄ˆκΈ°ν™”
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		
		// 2. ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€ 순회
		for (int i = 0; i < t; i++) {
			// μ§€μ—­ λ³€μˆ˜ μ„ μ–Έ 및 μ΄ˆκΈ°ν™”
			StringTokenizer st = new StringTokenizer(br.readLine());
			int x = Integer.parseInt(st.nextToken());
			int y = Integer.parseInt(st.nextToken());
			int distance = y - x;
			int move = 1, count = 0, current = 0;
			
			// 이동 처리
			while (current < distance) {
				if (distance - current <= move) {
					count++;
					break;
				} else if (distance - current < move * 2) {
					count += 2;
					break;
				} else {
					count += 2;
					current += move++ * 2;
				}
			}
			
			// 좜λ ₯
			System.out.println(count);
		}
	}
}
  • κ·œμΉ™μ„ μ°Ύμ•„μ„œ Brute-force λ°©μ‹μœΌλ‘œ ν’€μ—ˆλ‹€.

회고

  • μ‹œκ°„ λ‚  λ•Œ 더 λ‚˜μ€ 방법이 μžˆλŠ”μ§€ 생각해보아야겠닀.
This post is licensed under CC BY 4.0 by the author.