본문 바로가기

알고리즘/초급1

[JAVA] 백준 1107번 : 리모컨 ( 초급 2-4 ) - hard

문제

수빈이는 TV를 보고 있다. 수빈이는 채널을 돌리려고 했지만, 버튼을 너무 세게 누르는 바람에, 일부 숫자 버튼이 고장났다.

리모컨에는 버튼이 0부터 9까지 숫자, +와 -가 있다. +를 누르면 현재 보고있는 채널에서 +1된 채널로 이동하고, -를 누르면 -1된 채널로 이동한다. 채널 0에서 -를 누른 경우에는 채널이 변하지 않고, 채널은 무한대 만큼 있다.

수빈이가 지금 이동하려고 하는 채널은 N이다. 어떤 버튼이 고장났는지 주어졌을 때, 채널 N으로 이동하기 위해서 버튼을 최소 몇 번 눌러야하는지 구하는 프로그램을 작성하시오. 

수빈이가 지금 보고 있는 채널은 100번이다.

입력

첫째 줄에 수빈이가 이동하려고 하는 채널 N (0 ≤ N ≤ 500,000)이 주어진다.  둘째 줄에는 고장난 버튼의 개수 M (0 ≤ M ≤ 10)이 주어진다. 고장난 버튼이 있는 경우에는 셋째 줄에는 고장난 버튼이 주어지며, 같은 버튼이 여러 번 주어지는 경우는 없다.

출력

첫째 줄에 채널 N으로 이동하기 위해 버튼을 최소 몇 번 눌러야 하는지를 출력한다.

 

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    private static List<Integer> brokenButtoninfo;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int num = sc.nextInt();
        int n = sc.nextInt();
        brokenButtoninfo = new ArrayList<>();

        for (int i = 0; i < n; i++) {
            brokenButtoninfo.add(sc.nextInt());
        }

        int res = Math.abs(100 - num);
        int plusNum = num;
        int minusNum = num;
        int channel = 1000000;

        while (true) {
            if (Math.min(Math.abs(plusNum - num), Math.abs(num - minusNum)) > res) {
                break;
            }
            if (minusNum >= 0 && isContainBrokenButton(minusNum)) {
                channel = minusNum;
                break;
            }
            if (plusNum <= 1_000_000 && isContainBrokenButton(plusNum)) {
                channel = plusNum;
                break;
            }
            
            
            
            plusNum += 1;
            minusNum -= 1;
        }
        int length = Integer.toString(channel).length();
        if (res > Math.abs(channel - num) + length) {
            res = Math.abs(channel - num) + length;
        }
        System.out.println(res);
    }

    private static boolean isContainBrokenButton(Integer buttonInfo) {
        String[] buttons = Integer.toString(buttonInfo).split("");
        for (String button : buttons) {
            if (brokenButtoninfo.contains(Integer.parseInt(button))) {
                return false;
            }
        }
        return true;
    }
}

 

 

import java.util.Scanner;

public class baekjoon1107_3 {
    private static int[] brokenButtoninfo;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int targetChannel = sc.nextInt();
        int n = sc.nextInt();
        brokenButtoninfo = new int[10];
        ;
        for (int i = 0; i < n; i++) {
            brokenButtoninfo[sc.nextInt()] = 1;
        }

        int distance = Math.abs(100 - targetChannel);

        for (int i = 0; i < 1_000_000; i++) {
            int length = getLength(i);
            if (length > 0) {
                distance = Math.min(Math.abs(i - targetChannel) + length, distance);
            }
        }
        System.out.println(distance);
    }

    static int getLength(int num) {
        int length = 0;

        if (num == 0) {
            return brokenButtoninfo[0] == 1 ? 0 : 1;
        }

        while (num > 0) {
            if (brokenButtoninfo[num % 10] == 1) {
                return 0;
            }
            length++;
            num /= 10;
        }

        return length;
    }
}