-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_1107.cpp
More file actions
56 lines (47 loc) · 837 Bytes
/
BOJ_1107.cpp
File metadata and controls
56 lines (47 loc) · 837 Bytes
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
55
56
// 21/10/24
#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;
bool check[10];
int answer = 987654321;
int target=0, level=1;
void DFS(int num, int lev)
{
answer = min(answer, lev + abs(num-target));
if (lev == level)
return;
for (int i=0; i<10; i++)
{
if (check[i])
continue;
DFS(num*10+i, lev+1);
}
}
int main(void)
{
cin >> target;
int n;
cin >> n;
for (int i=0; i<n; i++)
{
int x;
cin >> x;
check[x] = true;
}
int temp = target;
while (temp > 0)
{
temp /= 10;
level ++;
}
answer = min(answer, abs(target-100));
for (int i=0; i<10; i++)
{
if (check[i])
continue;
DFS(i, 1);
}
cout << answer;
return 0;
}