[Gold V] 리모컨 - 1107

문제 링크

성능 요약

메모리: 2028 KB, 시간: 56 ms

분류

브루트포스 알고리즘

🥴 Hmm...

일단 100에서 채널버튼으로 가는게 더 짧다면 부조건 그게 답이다.
그렇지 않다면.. 음..
번호 사이즈가 곧 번호 누르는 카운트 개수에다가, 채널 옮겨지는 횟수를 더해야 하는데
결국 우리가 알아야 할거는 N과 가장 가까우면서 고장난 버튼이 포함되지 않은 수 만 찾으면된다!

🖥️ 소스코드

#define fastio ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <string.h>
using namespace std;

/* Method Initialization */
void input();
void solve();
int check(string num);

/* Variable Initialization */
string n;
int m, b, res;
int button[10];


int main()
{
    fastio;
    input();
    solve();
    return 0;
}

void input(){
    cin >> n;
    cin >> m;
    for(int i = 0 ; i < m; i++){
        cin >> b;
        button[b] = 1;
    }
}

void solve() {
    if (stoi(n) == 100){
        cout << 0 << '\n';
        return;
    }

    int from_100;
    from_100 = abs(stoi(n) - 100);
    int cnt = 0;
    string up = n;
    string down = n;
    while(1){
        if(cnt + up.size() > from_100){
            cnt = from_100;
            break;
        }
        if(check(down) && stoi(down) >= 0){
            cnt += down.size();
            break;
        }
        else if (check(up)){
            cnt += up.size();
            break;
        }
        up = to_string(stoi(up) + 1);
        down = to_string(stoi(down) - 1);
        cnt++;
    }
    cout << cnt << '\n';
}

int check(string num){
    for(int i = 0 ; i < num.size(); i++){
        if(button[num[i] - '0']) return 0;
    }
    return 1;
}
profile
언젠간 전문가가 되겠지

0개의 댓글

Powered by GraphCDN, the GraphQL CDN