-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1107.cpp
45 lines (41 loc) · 830 Bytes
/
1107.cpp
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
#include <algorithm>
#include <iostream>
using namespace std;
bool broken[10];
int abs(int a) { return a >= 0 ? a : -a; }
int length(int a) {
int d = 10;
int i = 1;
while(1){
if(a < d)
return i;
d*=10; i++;
}
}
bool CanPressButton(int n) {
if (n == 0 && broken[0]) {
return false;
}
while (0 < n) {
if (broken[n % 10]) return false;
n /= 10;
}
return true;
}
int main() {
int to, from = 100, m, ans = 0x3f3f3f3f;
cin >> to >> m;
while (m--) {
int k;
cin >> k;
broken[k] = true;
}
for (int i = 0; i <= 1000000; i++) {
if (CanPressButton(i)) {
ans = min(ans, length(i) + abs(to - i));
}
}
ans = min(ans, abs(to - 100));
cout << ans << '\n';
return 0;
}