Published 2021. 1. 19. 18:30
 

1107번: 리모컨

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

www.acmicpc.net

#include <iostream>
using namespace std;
bool broken[10];
int possible(int c) {
    if (c == 0) {
        if (broken[0]) {
            return 0;
        } else {
            return 1;
        }
    }
    int len = 0;
    while (c > 0) {
        if (broken[c % 10]) {
            return 0;
        }
        len += 1;
        c /= 10;
    }
    return len;
}
int main() {
    int n;
    cin >> n;
    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
        int x;
        cin >> x;
        broken[x] = true;
    }
    int ans = n - 100;
    if (ans < 0) {
        ans = -ans;
    }
    for (int i = 0; i <= 1000000; i++) {
        int c = i;
        int len = possible(c);
        if (len > 0) {
            int press = c - n;
            if (press < 0) {
                press = -press;
            }
            if (ans > len + press) {
                ans = len + press;
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

'Problem set' 카테고리의 다른 글

[백준] 6064 카잉 달력  (0) 2021.01.19
[백준] 14500 테트로미노  (0) 2021.01.19
[백준] 1476 날짜 계산  (0) 2021.01.19
[백준] 3085 사탕 게임  (0) 2021.01.19
[백준] 2309 일곱 난쟁이  (0) 2021.01.19
복사했습니다!