-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1107번.swift
54 lines (48 loc) · 1.26 KB
/
1107번.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// 출처 : 백준 리모컨
// https://www.acmicpc.net/problem/1107
// 풀이 : hogumachu
// 뻘짓 너무 많이 했음
// 케이스 여러 개를 찾지 말고
// 브루트포스 방식으로 해결하기
// 수가 크지 않았을 때는 비효율적으로 해결해도 가능한지 생각하기
import Foundation
func solution() {
let now = 100
let N = Int(readLine()!)!
let M = Int(readLine()!)!
var notButton: [Int] = []
if M != 0 {
notButton = readLine()!.split(separator: " ").map{Int($0)!}
}
var result = abs(N-now)
func recursive(_ num: Int) {
if num.digit >= 6 {
return
}
for i in 0...9 {
if notButton.contains(i) == false {
let compare = num*10 + i
if compare == 0 {
result = min(result, (abs(N - compare)+1))
} else {
result = min(result, (abs(N - compare)+compare.digit))
recursive(compare)
}
}
}
}
recursive(0)
print(result)
}
extension Int {
var digit: Int {
var n = self
var count = 0
while n > 0 {
n /= 10
count += 1
}
return count
}
}
solution()