Skip to content

Commit 6f05a5e

Browse files
committed
[Gold V] Title: 리모컨, Time: 556 ms, Memory: 287024 KB -BaekjoonHub
1 parent 2d6768a commit 6f05a5e

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [Gold V] 리모컨 - 1107
2+
3+
[문제 링크](https://www.acmicpc.net/problem/1107)
4+
5+
### 성능 요약
6+
7+
메모리: 287024 KB, 시간: 556 ms
8+
9+
### 분류
10+
11+
브루트포스 알고리즘
12+
13+
### 제출 일자
14+
15+
2025년 3월 26일 11:32:09
16+
17+
### 문제 설명
18+
19+
<p>수빈이는 TV를 보고 있다. 수빈이는 채널을 돌리려고 했지만, 버튼을 너무 세게 누르는 바람에, 일부 숫자 버튼이 고장났다.</p>
20+
21+
<p>리모컨에는 버튼이 0부터 9까지 숫자, +와 -가 있다. +를 누르면 현재 보고있는 채널에서 +1된 채널로 이동하고, -를 누르면 -1된 채널로 이동한다. 채널 0에서 -를 누른 경우에는 채널이 변하지 않고, 채널은 무한대 만큼 있다.</p>
22+
23+
<p>수빈이가 지금 이동하려고 하는 채널은 N이다. 어떤 버튼이 고장났는지 주어졌을 때, 채널 N으로 이동하기 위해서 버튼을 최소 몇 번 눌러야하는지 구하는 프로그램을 작성하시오. </p>
24+
25+
<p>수빈이가 지금 보고 있는 채널은 100번이다.</p>
26+
27+
### 입력
28+
29+
<p>첫째 줄에 수빈이가 이동하려고 하는 채널 N (0 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 고장난 버튼의 개수 M (0 ≤ M ≤ 10)이 주어진다. 고장난 버튼이 있는 경우에는 셋째 줄에는 고장난 버튼이 주어지며, 같은 버튼이 여러 번 주어지는 경우는 없다.</p>
30+
31+
### 출력
32+
33+
<p>첫째 줄에 채널 N으로 이동하기 위해 버튼을 최소 몇 번 눌러야 하는지를 출력한다.</p>
34+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
static Set<Integer> destroyedButton;
6+
static int answer;
7+
static int N,M;
8+
public static void main(String[] args) throws IOException {
9+
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
12+
N = Integer.parseInt(br.readLine());
13+
M = Integer.parseInt(br.readLine());
14+
15+
destroyedButton = new HashSet<>();
16+
if(M>0) {
17+
StringTokenizer st = new StringTokenizer(br.readLine());
18+
for(int i = 0; i<M; i++) {
19+
int but = Integer.parseInt(st.nextToken());
20+
destroyedButton.add(but);
21+
}
22+
}
23+
24+
25+
if(N ==100) {
26+
System.out.println(0);
27+
return;
28+
}
29+
answer = Math.abs(N -100);
30+
31+
dfs(0,0);
32+
System.out.println(answer);
33+
}
34+
35+
static void dfs(int index, int click) {
36+
37+
for(int i =0; i<10; i++) {
38+
if(!destroyedButton.contains(i)) {
39+
int newBtn = click * 10 + i;
40+
int cnt = Math.abs(N - newBtn) + String.valueOf(newBtn).length();
41+
answer = Math.min(cnt, answer);
42+
if(index< 6) {
43+
dfs(index+1, newBtn);
44+
}
45+
}
46+
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)