Wiki OI 1099 字串变换(双向BFS)

99 篇文章 0 订阅
37 篇文章 0 订阅

题目链接:http://wikioi.com/problem/1099/

算法与思路:双向广搜

所谓双向搜索指的是搜索沿两个方向同时进行:

正向搜索:从初始结点向目标结点方向搜索;
逆向搜索:从目标结点向初始结点方向搜索;

当两个方向的搜索生成同一子结点时终止此搜索过程。

详见注释。

  1. #include<stdio.h>  
  2. #include<string.h>  
  3. struct node  
  4. {  
  5.     char s[30];  
  6.     int dep;  //变换次数   
  7. } list1[5010], list2[5010];  
  8. char a[7][30], b[7][30];  
  9. int n;  
  10. void BFS()  
  11. {  
  12.     int head1, tail1, head2, tail2, k;  
  13.     head1 = tail1 = head2 = tail2 = 1;  
  14.     while(head1 <= tail1 && head2 <= tail2)  
  15.     {  
  16.         if(list1[head1].dep + list2[head2].dep > 10)   
  17.         {  
  18.             printf("NO ANSWER!\n");  
  19.             return ;  
  20.         }  
  21.         for(int i = 0;i < strlen(list1[head1].s); i++)  
  22.             for(int j = 1; j <= n; j++)  
  23.                 if(strncmp(list1[head1].s + i, a[j], strlen(a[j])) == 0) //寻找当前可变换的规则   
  24.                 {  
  25.                   tail1++; //移动尾指针,存储变换后的字符串,以下三个for循环为变换过程   
  26.                   for(k = 0; k < i; k++)   
  27.                       list1[tail1].s[k] = list1[head1].s[k];  
  28.                   for(int l = 0; l < strlen(b[j]); l++, k++)   
  29.                       list1[tail1].s[k] = b[j][l];  
  30.                   for(int l = i + strlen(a[j]); l <= strlen(list1[head1].s); l++, k++)  
  31.                      list1[tail1].s[k] = list1[head1].s[l];  
  32.                   list1[tail1].s[k] = '\0'//为变换结束后的字符串加结束符   
  33.                   list1[tail1].dep = list1[head1].dep+1;  
  34.                   for (k = 1; k <= tail1; k++)  
  35.                     if (strcmp(list1[tail1].s, list2[k].s) == 0)//判断当前状态是否与逆向搜索交汇   
  36.                     {  
  37.                        printf("%d\n", list1[tail1].dep + list2[k].dep);  
  38.                        return ;  
  39.                      }  
  40.                 }  
  41.         for (int i = 0; i < strlen(list2[head2].s); i++) //逆向搜索同上   
  42.             for (int j = 1; j <= n; j++)  
  43.                 if(strncmp(list2[head2].s + i, b[j], strlen(b[j])) == 0)  
  44.                 {  
  45.                   tail2++;  
  46.                   for(k = 0; k < i; k++)   
  47.                       list2[tail2].s[k] = list2[head2].s[k];  
  48.                   for(int l = 0; l < strlen(a[j]); l++, k++)   
  49.                       list2[tail2].s[k] = a[j][l];  
  50.                   for(int l = i + strlen(b[j]); l <= strlen(list2[head2].s); l++, k++)  
  51.                     list2[tail2].s[k] = list2[head2].s[l];  
  52.                   list2[tail2].s[k] = '\0';  
  53.                   list2[tail2].dep = list2[head2].dep + 1;  
  54.                   for (k = 1;k <= tail1; k++)  
  55.                     if (strcmp(list1[k].s, list2[tail2].s) == 0)  
  56.                     {  
  57.                        printf("%d\n",list1[k].dep + list2[tail2].dep);  
  58.                        return ;  
  59.                     }  
  60.                 }  
  61.         head1++;   
  62.         head2++;  
  63.     }  
  64.     printf("NO ANSWER!\n");  
  65. }  
  66. int main()  
  67. {  
  68.     scanf("%s%s",list1[1].s, list2[1].s);  
  69.     n = 1;  
  70.     while (scanf("%s%s",a[n],b[n]) != EOF)   
  71.         n++;  
  72.     n--;  
  73.     list1[1].dep = list2[1].dep = 0;  
  74.     BFS();  
  75.     return 0;  
  76. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值