http://ybt.ssoier.cn:8088/problem_show.php?pid=1361
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int M=20; 4 int n,k,a[M],b[M]; 5 bool flag[100005]; 6 7 int q[200005]; 8 int f, r; 9 int ans; 10 11 void bfs() 12 { 13 f=r=1; 14 ans++; 15 flag[n]=1; 16 q[r]=n; 17 while(f<=r){ 18 int x=q[f],y=q[f],mod=1; 19 while(x>0){ 20 int xx=x%10;x/=10; 21 for(int i=1;i<=k;i++){ 22 int yy=y+(b[i]-a[i])*mod; 23 if(xx==a[i]&&!flag[yy]){ 24 flag[yy]=1; 25 ans++; 26 q[++r]=yy; 27 } 28 } 29 mod*=10; 30 } 31 f++; 32 } 33 } 34 35 int main() 36 { 37 cin>>n>>k; 38 for(int i=1;i<=k;i++) 39 scanf("%d%d",&a[i],&b[i]); 40 bfs(); 41 printf("%d",ans); 42 return 0; 43 }