http://acm.pku.edu.cn/JudgeOnline/problem?id=3087这道题目在网上的一些分类中把它分在BFS的范围,可是只需用程序模拟出题目中说明的过程,就可以直接得出结果了c
1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4 int n,m;
5 char s1[101],s2[101],s[201];
6 int process();
7 int main()
8 {
9 scanf("%d",&n);
10 int i = 1;
11 while(n--){
12 scanf("%d %s %s %s",&m,s1,s2,s);
13 printf("%d %d\n",i++,process());
14 }
15 system("pause");
16 return 0;
17 }
18
19 int process()
20 {
21 int i;
22 int step = 1;
23 char cmp[201],p1[101],p2[101];
24 strcpy(p1,s1);
25 strcpy(p2,s2);
26 int a = 2;
27 while(1){
28 for(i = 0;i < m;i++){
29 cmp[i*2] = p2[i];
30 }
31 for(i = 0;i < m;i++){
32 cmp[i*2+1] = p1[i];
33 }
34 cmp[2*m] = '\0';
35 if(!strcmp(cmp,s))return step;
36 for(i = 0;i < m;i++){
37 p1[i] = cmp[i];
38 p2[i] = cmp[i+m];
39 }
40 p1[m] = '\0';
41 p2[m] = '\0';
42 if(!strcmp(p1,s1) && !strcmp(p2,s2))break;
43 step++;
44 }
45 return -1;
46 }
47
ode