给出一个整数n(n<=2000)(代码可适用n<=10^31)和k个变换规则(k<=15)。
规则:1、1个数字可以变换成另1个数字;
2、规则中右边的数字不能为零。
BFS
#include <stdio.h>
#include <string.h>
#define maxn 1000 char num[];
int len,q[maxn],Visited[];
long long ans = ; int main (){
// freopen ("produce.in","r",stdin);
// freopen ("produce.out","w",stdout); int i,j,k;
int K,x[],y[]; scanf ("%s%d",num,&K);
for (i = ;i<=K;i++)
scanf ("%d%d",x+i,y+i);
len = strlen (num); int head = ,tail = ,temp; for (j = ;j<len;j++){
temp = ;
memset (Visited,,sizeof(Visited));
q[++tail] = num[j]-'';
do{
head++;
for (i = ;i<=K;i++){
if (q[head] == x[i] && Visited[y[i]] == ){
q[++tail] = y[i];
temp++;
Visited[x[i]] = ;
Visited[y[i]] = ;
} }
}while (head<tail);
ans*=temp;
} printf ("%lld",ans); return ;
}