알고리즘/백준(BOJ)

백준 - 1107 리모컨

시나모온 2020. 11. 24. 16:37

문제 링크입니다 : www.acmicpc.net/problem/1107

 

1107번: 리모컨

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

www.acmicpc.net

 

브루트 포스 문제였다.

 

채널이 500,000이므로 브루트 포스를 돌려도 충분히 시간 안에 해결이 가능한 문제라서 브루트 포스로 풀었다.

 

 

 

 

#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <string>
#define START 100

using namespace std;


int main() {
    int N, M;
    cin >> N >> M;

    vector<bool> isDisable(10, false);

    int a;
    for(int i = 0; i < M; i++) {
        cin >> a;
        isDisable[a] = true;
    }


    int minVal = 1000000;
    for(int i = 0; i <= 1000000; i++) {
        int term = abs(N - START);

        bool canMake = true;
        string str = to_string(i);

        for(int j = 0; j < str.size(); j++) {
            if(isDisable[str[j] - '0']) {
                canMake = false;
                break;
            }
        }

        if(canMake) {
            term = min(term, (int)str.size() + abs(N - i));
        }

        if(term < minVal) {
            minVal = term;
        }

    }

    cout << minVal;

    return 0;
}

 

 

 

N = int(input())
M = int(input())
list_disable = []
if M > 0:
    list_disable = map(int, input().split())
is_enable = [True] * 10

for i, x in enumerate(list_disable):
    is_enable[x] = False

min_val = float('inf')
for num in range(1000001):
    term = abs(N - 100)

    can_make_flag = True
    s = str(num)
    for c in s:
        if not is_enable[ord(c) - ord('0')]:
            can_make_flag = False
            break
    
    if can_make_flag:
        term = min(term, len(s) + abs(N - num))
    
    if term < min_val:
        min_val = term

print(min_val)

 

 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		Scanner sc = new Scanner(System.in);
		
		int N = sc.nextInt();
		int M = sc.nextInt();
		
		boolean[] isDisable = new boolean[10];
		if(M > 0) {
			for(int i = 0; i < M; i++) {				
				isDisable[sc.nextInt()] = true;
			}
		}
		
		int minVal = 1000000;
		
		for(int i = 0; i <= 1000000; i++) {
			int term = Math.abs(N - 100);
			
			String str = String.valueOf(i);

			boolean canMake = true;
			for(int j = 0; j < str.length(); j++) {
				if(isDisable[str.charAt(j) - '0']) {
					canMake = false;
					break;
				}
			}
			
			if(canMake) {
				term = Math.min(term, str.length() + Math.abs(N - i));
			}
			
			if(term < minVal) {
				minVal = term;
			}
			
		}
		
		System.out.println(minVal);
		
		

		bw.flush();
		bw.close();
		
		
		br.close();
    }
}

 

 

개발 환경 : vscode, eclipse 2020-03, C++ 11, Python3, Java 11

 

지적, 조언, 질문 환영입니다! 댓글 남겨주세요~

 

 

 

 

 

개발 환경 : vscode

지적, 조언, 질문 환영입니다! 댓글 남겨주세요~