![[백준 1107번 / Java] 리모컨 - Brute Force(DFS)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FeaMhm0%2FbtsmkIYmuEl%2Fovp2jB43D4hJlQ8q9L0Gh0%2Fimg.png)
[백준 1107번 / Java] 리모컨 - Brute Force(DFS)코딩테스트/백준2023. 7. 4. 00:50
Table of Contents
728x90
728x90
문제 링크
https://www.acmicpc.net/problem/1107
1107번: 리모컨
첫째 줄에 수빈이가 이동하려고 하는 채널 N (0 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 고장난 버튼의 개수 M (0 ≤ M ≤ 10)이 주어진다. 고장난 버튼이 있는 경우에는 셋째 줄에는 고장난 버튼
www.acmicpc.net
풀이
1. 500,000까지의 채널이 존재했고, 가장 큰 경우가 999,999의 경우를 고려하여 반복문의 최대 길이를 설정
2. answer는 단순 +,-로 해당 채널로 이동하는 경우, 버튼을 눌리는 횟수
3. length는 해당 버튼의 길이. 즉 해당 채널로 이동하기 위한 버튼을 눌리는 횟수
4. 모든 경우의 수에서 최소값을 구함
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BaekJoon1107 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int channel = Integer.parseInt(br.readLine());
int brokenBtn = Integer.parseInt(br.readLine());
int[] brokenBtnArr = new int[10];
if(brokenBtn > 0){
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for(int i = 0; i < brokenBtn; i++) {
int number = Integer.parseInt(st.nextToken());
brokenBtnArr[number]--;
}
}
if(channel - 100 == 0) {
System.out.println(0);
return;
}
//+- 눌려서 이동하는 거리
int answer = Math.abs(channel - 100);
StringBuffer sb = new StringBuffer();
for(int i = 0; i < 1000000; i++) {
sb.append(String.valueOf(i));
int length = sb.length();
boolean chk = false;
for(int j = 0; j < length; j++) {
if(brokenBtnArr[sb.charAt(j) - '0'] == -1) {
chk = true;
break;
}
}
if(!chk) {
//+-이동과 버튼 눌렸을 때의 합
answer = Math.min(Math.abs(channel - i) + length, answer);
}
sb.setLength(0);
}
System.out.println(answer);
}
}
728x90
300x250
@mag1c :: 꾸준히 재밌게
2023.04 ~ 백엔드 개발자의 기록
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!