-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1107.cpp
43 lines (35 loc) · 871 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
#include <cstdio>
#include <queue>
using namespace std;
int d[1000001],n,m,x;
bool b[10];
priority_queue<pair<int, int>> q;
void insertChannel(int k, int channel){
if(k==6) return;
for(int i=9; i>=0; --i)
if(!b[i] && !d[channel+i]){
q.push({-k-2, channel+i});
insertChannel(k+1, (channel+i)*10);
}
return;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<m;++i){
scanf("%d",&x);
b[x]=1;
}
insertChannel(0,0);
q.push({-1, 100});
while(!q.empty()){
auto [cnt, channel] = q.top(); q.pop();
if(d[channel]) continue;
d[channel] = -cnt;
if(channel-1>=0 && !d[channel-1])
q.push({cnt-1, channel-1});
if(channel+1<1000000 && !d[channel+1])
q.push({cnt-1, channel+1});
}
printf("%d",d[n]-1);
return 0;
}