[Informatics ausai yiben Tong] Part 3: Number of ex2_3produce queues,
Returns an integer n (n <= 2000) (the Code applies to n <= 10 ^ 31) and k Transformation Rules (k <= 15 ).
Rule: 1. One digit can be converted into another digit;
2. the number on the right of the rule cannot be zero.
BFS
1 #include <stdio.h> 2 #include <string.h> 3 #define maxn 1000 4 5 char num[33]; 6 int len,q[maxn],Visited[11]; 7 long long ans = 1; 8 9 int main (){10 // freopen ("produce.in","r",stdin);11 // freopen ("produce.out","w",stdout); 12 13 int i,j,k;14 int K,x[16],y[16];15 16 scanf ("%s%d",num,&K);17 for (i = 1;i<=K;i++)18 scanf ("%d%d",x+i,y+i);19 len = strlen (num); 20 21 int head = 0,tail = 0,temp;22 23 for (j = 0;j<len;j++){24 temp = 1;25 memset (Visited,0,sizeof(Visited));26 q[++tail] = num[j]-'0';27 do{28 head++;29 for (i = 1;i<=K;i++){30 if (q[head] == x[i] && Visited[y[i]] == 0){31 q[++tail] = y[i];32 temp++; 33 Visited[x[i]] = 1;34 Visited[y[i]] = 1;35 }36 37 }38 }while (head<tail);39 ans*=temp;40 }41 42 printf ("%lld",ans);43 44 return 0;45 }