Wiki OI 1099 字串变换

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

算法与思路:双向广搜

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

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

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

详见注释。

#include<stdio.h>
#include<string.h>
struct node
{
    char s[30];
    int dep;  //变换次数 
} list1[5010], list2[5010];
char a[7][30], b[7][30];
int n;
void BFS()
{
    int head1, tail1, head2, tail2, k;
    head1 = tail1 = head2 = tail2 = 1;
    while(head1 <= tail1 && head2 <= tail2)
    {
        if(list1[head1].dep + list2[head2].dep > 10) 
        {
            printf("NO ANSWER!\n");
            return ;
        }
        for(int i = 0;i < strlen(list1[head1].s); i++)
            for(int j = 1; j <= n; j++)
                if(strncmp(list1[head1].s + i, a[j], strlen(a[j])) == 0) //寻找当前可变换的规则 
                {
                  tail1++; //移动尾指针,存储变换后的字符串,以下三个for循环为变换过程 
                  for(k = 0; k < i; k++) 
				      list1[tail1].s[k] = list1[head1].s[k];
                  for(int l = 0; l < strlen(b[j]); l++, k++) 
				      list1[tail1].s[k] = b[j][l];
                  for(int l = i + strlen(a[j]); l <= strlen(list1[head1].s); l++, k++)
                     list1[tail1].s[k] = list1[head1].s[l];
                  list1[tail1].s[k] = '\0'; //为变换结束后的字符串加结束符 
                  list1[tail1].dep = list1[head1].dep+1;
                  for (k = 1; k <= tail1; k++)
                    if (strcmp(list1[tail1].s, list2[k].s) == 0)//判断当前状态是否与逆向搜索交汇 
                    {
                       printf("%d\n", list1[tail1].dep + list2[k].dep);
                       return ;
                     }
                }
        for (int i = 0; i < strlen(list2[head2].s); i++) //逆向搜索同上 
            for (int j = 1; j <= n; j++)
                if(strncmp(list2[head2].s + i, b[j], strlen(b[j])) == 0)
                {
                  tail2++;
                  for(k = 0; k < i; k++) 
				      list2[tail2].s[k] = list2[head2].s[k];
                  for(int l = 0; l < strlen(a[j]); l++, k++) 
				      list2[tail2].s[k] = a[j][l];
                  for(int l = i + strlen(b[j]); l <= strlen(list2[head2].s); l++, k++)
                    list2[tail2].s[k] = list2[head2].s[l];
                  list2[tail2].s[k] = '\0';
                  list2[tail2].dep = list2[head2].dep + 1;
                  for (k = 1;k <= tail1; k++)
                    if (strcmp(list1[k].s, list2[tail2].s) == 0)
                    {
                       printf("%d\n",list1[k].dep + list2[tail2].dep);
                       return ;
                    }
                }
        head1++; 
		head2++;
    }
    printf("NO ANSWER!\n");
}
int main()
{
    scanf("%s%s",list1[1].s, list2[1].s);
    n = 1;
    while (scanf("%s%s",a[n],b[n]) != EOF) 
	    n++;
	n--;
    list1[1].dep = list2[1].dep = 0;
    BFS();
    return 0;
} 


你可能感兴趣的:(Wiki OI 1099 字串变换)