Chinaunix首页 | 论坛 | 博客
  • 博客访问: 17436
  • 博文数量: 12
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 130
  • 用 户 组: 普通用户
  • 注册时间: 2013-04-04 10:14
文章分类
文章存档

2013年(12)

我的朋友

分类: C/C++

2013-12-04 14:48:23

我采用的是:广度优先搜索
已知图G=(V,E)和一个源顶点s,宽度优先搜索以一种系统的方式探寻G的边,从而发现”s所能到达的所有顶点,并计算s到所有这些顶点的距离(最少边数),该算法同时能生成一棵根为s且包括所有可达顶点的宽度优先树。对从s可达的任意顶点v,宽度优先树中从sv的路径对应于图G中从sv的最短路径,即包含最小边数的路径。该算法对有向图和无向图同样适用。

之所以称之为宽度优先算法,是因为算法自始至终一直通过已找到和未找到顶点之间的边界向外扩展,就是说,算法首先搜索和s距离为k的所有顶点,然后再去搜索和S距离为k+l的其他顶点。

为了保持搜索的轨迹,宽度优先搜索为每个顶点着色:白色、灰色或黑色。算法开始前所有顶点都是白色,随着搜索的进行,各顶点会逐渐变成灰色,然后成为黑色。在搜索中第一次碰到一顶点时,我们说该顶点被发现,此时该顶点变为非白色顶点。因此,灰色和黑色顶点都已被发现,但是,宽度优先搜索算法对它们加以区分以保证搜索以宽度优先的方式执行。若(u,v)∈E且顶点u为黑色,那么顶点v要么是灰色,要么是黑色,就是说,所有和黑色顶点邻接的顶点都已被发现。灰色顶点可以与一些白色顶点相邻接,它们代表着已找到和未找到顶点之间的边界。

在宽度优先搜索过程中建立了一棵宽度优先树,起始时只包含根节点,即源顶点s.在扫描已发现顶点u的邻接表的过程中每发现一个白色顶点v,该顶点v及边(u,v)就被添加到树中。在宽度优先树中,我们称结点u 是结点v的先辈或父母结点。因为一个结点至多只能被发现一次,因此它最多只能有--个父母结点。相对根结点来说祖先和后裔关系的定义和通常一样:如果u处于树中从根s到结点v的路径中,那么u称为v的祖先,vu的后裔。




main.c:

点击(此处)折叠或打开

  1. //路径搜索 程序入口
  2. //2013.12
  3. //santhinking
  4. //ecit


  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. #include<string.h>
  8. #include"myhead.h"

  9. void get_msg(const char *str,char *file,int *begin,int *end,int *c_num,char *out);

  10. //int change_num(char *s);


  11. int main(int argc,char *argv[])
  12. {
  13.     char file[30];
  14.     char out[30];
  15.     int begin,end;
  16.     int c_num;

  17.     //char str[]="/fBigdata.txt/s91/d93/c2/oout.txt";
  18.     //printf("%s\n",argv[1]);

  19.     if(argc!=2)
  20.     {
  21.         printf("usage:demo.exe /fBigdata.txt/s145/d258/c1/oout.txt\n");
  22.         exit(0);
  23.     }
  24.     get_msg(argv[1],file,&begin,&end,&c_num,out);
  25.     //get_msg(str,file,&begin,&end,&c_num,out);

  26.     get_way(file,begin,end,c_num,out);

  27.     return 0;
  28. }

  29. void get_msg(const char *str,char *file,int *begin,int *end,int *c_num,char *out)
  30. {
  31.     char msg[5][30];
  32.     int i=0;
  33.     int n=0;
  34.     int j=0;
  35.     
  36.     //printf("%d\n",strlen(str));

  37.     for(i;i<strlen(str);i++)
  38.     {
  39.         if(str[i]=='/'&&str[i+1]=='f')
  40.         {
  41.             j=0;
  42.             for(n=i+2;n<strlen(str);n++)
  43.             {
  44.                 if(str[n]=='/'||str[n]=='\0')
  45.                     break;
  46.                 file[j]=str[n];
  47.                 j++;
  48.             }
  49.             file[j]='\0';
  50.         }
  51.         if(str[i]=='/'&&str[i+1]=='s')
  52.         {
  53.             j=0;
  54.             for(n=i+2;n<strlen(str);n++)
  55.             {
  56.                 if(str[n]=='/'||str[n]=='\0')
  57.                     break;
  58.                 msg[1][j]=str[n];
  59.                 j++;
  60.             }
  61.             msg[1][j]='\0';
  62.         }
  63.         if(str[i]=='/'&&str[i+1]=='d')
  64.         {
  65.             j=0;
  66.             for(n=i+2;n<strlen(str);n++)
  67.             {
  68.                 if(str[n]=='/'||str[n]=='\0')
  69.                     break;
  70.                 msg[2][j]=str[n];
  71.                 j++;
  72.             }
  73.             msg[2][j]='\0';
  74.         }
  75.         if(str[i]=='/'&&str[i+1]=='c')
  76.         {
  77.             j=0;
  78.             for(n=i+2;n<strlen(str);n++)
  79.             {
  80.                 if(str[n]=='/'||str[n]=='\0')
  81.                     break;
  82.                 msg[3][j]=str[n];
  83.                 j++;
  84.             }
  85.             msg[3][j]='\0';
  86.         }
  87.         if(str[i]=='/'&&str[i+1]=='o')
  88.         {
  89.             j=0;
  90.             for(n=i+2;n<strlen(str);n++)
  91.             {
  92.                 if(str[n]=='/'||str[n]=='\0')
  93.                     break;
  94.                 out[j]=str[n];
  95.                 j++;
  96.             }
  97.             out[j]='\0';
  98.         }
  99.     }
  100.     *begin=change(msg[1]);
  101.     *end=change(msg[2]);
  102.     *c_num=change(msg[3]);
  103. }

  104. /*int change_num(char *s)
  105. {
  106.     int num=0;
  107.     int i;
  108.     for(i=0;i<strlen(s);i++)
  109.     {
  110.      if(s[i]<'0'||s[i]>'9')
  111.             continue;
  112.         else
  113.         {
  114.             num=num*10;
  115.             switch(s[i])
  116.             {
  117.             case '0':num+=0;
  118.                 break;
  119.             case '1':num+=1;
  120.                 break;
  121.             case '2':num+=2;
  122.                 break;
  123.             case '3':num+=3;
  124.                 break;
  125.             case '4':num+=4;
  126.                 break;
  127.             case '5':num+=5;
  128.                 break;
  129.             case '6':num+=6;
  130.                 break;
  131.             case '7':num+=7;
  132.                 break;
  133.             case '8':num+=8;
  134.                 break;
  135.             case '9':num+=9;
  136.                 break;
  137.             }
  138.         }
  139.     }
  140.     return num;
  141. }*/
test.c:

点击(此处)折叠或打开

  1. //路径搜索 主算法实现
  2. //2013.12
  3. //santhinking
  4. //ecit
  5. //关于备用路径c2算法有问题

  6. #include <stdio.h>
  7. #include <math.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. //#include "struct_data.h"


  11. //邻接表数据结构 定义
  12. typedef struct node{            //节点结构
  13.     int nnum;                    //节点数据
  14.     struct node *nnext;            //下一节点指针
  15. }node;

  16. typedef struct head{            //头结构
  17.     int hnum;                    //节点数据
  18.     struct head *hnext;            //头节点指针
  19.     struct node *nnext;            //指向节点指针
  20. }head;
  21. //邻接表数据定义 结束

  22. //定义open表结构

  23. typedef struct farth{            //存储父节点数据结构
  24.     int num;
  25.     struct farth *next;
  26. }farth;

  27. typedef struct open{
  28.     int num;                    //节点数据
  29.     struct farth *f;
  30.     struct open *next;
  31. }open;

  32. //定义open表结束

  33. void fun2(char num[],char ch)
  34. {
  35.     int i=strlen(num);
  36.     //printf("%d %d\n",strlen(num),sizeof(num));
  37.     
  38.     if(strlen(num)<sizeof(num))
  39.     {
  40.         num[i]=ch;
  41.         num[i+1]='\0';
  42.     }

  43.     //return num;
  44. }

  45. int change(char *ch)
  46. {
  47.     int num=0;
  48.     int i;
  49.     for(i=0;i<strlen(ch);i++)
  50.     {
  51.      if(ch[i]<'0'||ch[i]>'9')
  52.             continue;
  53.         else
  54.         {
  55.             num=num*10;
  56.             switch(ch[i])
  57.             {
  58.             case '0':num+=0;
  59.                 break;
  60.             case '1':num+=1;
  61.                 break;
  62.             case '2':num+=2;
  63.                 break;
  64.             case '3':num+=3;
  65.                 break;
  66.             case '4':num+=4;
  67.                 break;
  68.             case '5':num+=5;
  69.                 break;
  70.             case '6':num+=6;
  71.                 break;
  72.             case '7':num+=7;
  73.                 break;
  74.             case '8':num+=8;
  75.                 break;
  76.             case '9':num+=9;
  77.                 break;
  78.             }
  79.         }
  80.     }
  81.     return num;
  82. }

  83. void add_num(head *hd,int num1,int num2)
  84. {
  85.     int a,b;

  86.     head *h1;
  87.     //static int count=0;
  88.     head *h2,*h3;
  89.     
  90.     node *n1,*n2,*n3;
  91.     n1=(node *)malloc(sizeof(node));
  92.     h1=(head *)malloc(sizeof(head));
  93.     a=b=0;
  94.     h1=hd;
  95.     h1->nnext=(node *)malloc(sizeof(node));
  96.     h1->nnext->nnum=0;
  97.     h1->nnext->nnext=NULL;

  98.     //if(count%2==0)
  99.     while(1)
  100.     {
  101.         if(h1->hnum==num1)
  102.         {
  103.             a=1;
  104.             n1=h1->nnext;
  105.             while(n1->nnext)
  106.             {
  107.                 n1=n1->nnext;    
  108.             }
  109.             if(!(n1->nnext))
  110.             {
  111.                 n2=(node *)malloc(sizeof(node));
  112.                 n2->nnum=num2;
  113.                 n2->nnext=NULL;

  114.                 n1->nnext=n2;
  115.                 //n1->nnext=NULL;
  116.                 //n1->nnum=num2;
  117.                 n1=n1->nnext;
  118.                 //free(n2);
  119.             }
  120.         }
  121.         if(h1->hnum==num2)
  122.         {
  123.             b=1;
  124.             n1=h1->nnext;
  125.             while(n1->nnext)
  126.             {
  127.                 n1=n1->nnext;    
  128.             }
  129.             if(!(n1->nnext))
  130.             {
  131.                 n3=(node *)malloc(sizeof(node));
  132.                 n3->nnum=num1;
  133.                 n3->nnext=NULL;

  134.                 n1->nnext=n3;
  135.                 //n1->nnext=NULL;
  136.                 //n1->nnum=num1;
  137.                 n1=n1->nnext;
  138.                 //free(n2);
  139.             }
  140.         }
  141.         if(a&&b)
  142.             break;
  143.         if(!h1->hnext)
  144.             break;
  145.         h1=h1->hnext;
  146.     }
  147.     if(a==0||b==0)
  148.     {
  149.         while(h1->hnext)
  150.             h1=h1->hnext;
  151.         if(!a)
  152.         {
  153.             h2=(head *)malloc(sizeof(head));
  154.             h2->hnum=num1;
  155.             h2->hnext=NULL;
  156.             h2->nnext=NULL;
  157.             h2->nnext=(node *)malloc(sizeof(node));
  158.             h2->nnext->nnext=NULL;
  159.             h2->nnext->nnum=0;

  160.             h1->hnext=h2;
  161.             
  162.             //free(h2);

  163.             h2->nnext->nnum=num2;
  164.         
  165.             h1=h1->hnext;
  166.         }
  167.         if(!b)
  168.         {
  169.             //h1=h1->hnext;
  170.             h3=(head *)malloc(sizeof(head));
  171.             h3->hnum=num2;
  172.             h3->hnext=NULL;
  173.             h3->nnext=NULL;
  174.             h3->nnext=(node *)malloc(sizeof(node));
  175.             h3->nnext->nnum=0;
  176.             h3->nnext->nnext=NULL;

  177.             h1->hnext=h3;
  178.             
  179.             //free(h3);
  180.             h3->nnext->nnum=num1;

  181.             
  182.             h1=h1->hnext;
  183.         }
  184.     }

  185. }


  186. void fun3(head *hd,int num)
  187. {
  188.     static int count=0;
  189.     static int num1;
  190.     if(count%2==0)
  191.         num1=num;
  192.     if(count%2==1)
  193.         add_num(hd,num1,num);

  194.     count ++;
  195. }


  196. int * main_way(open *hopen,open *hclosed,int begin,int end,int *main,int *main_way)
  197. {
  198.     int i=0;
  199.     open *op2;

  200.     //main_way=(int *)malloc(500*sizeof(int));
  201.     
  202.     main_way[i]=hclosed->num;
  203.     i++;
  204.     //printf("%d %d ",i++,hclosed->num);
  205.     

  206.     while(1)
  207.     {
  208.         if(hclosed->num==begin)
  209.             break;

  210.         op2=hopen;
  211.         while(1)
  212.         {
  213.             if(op2->num==hclosed->f->num)
  214.             {
  215.                 main_way[i]=op2->num;
  216.                 i++;
  217.                 //printf("\n%d %d",i++,op2->num);
  218.                 break;
  219.             }
  220.             op2=op2->next;
  221.         }
  222.         hclosed=op2;
  223.     }
  224.     i--;
  225.     *main=i;

  226.     return main_way;
  227. }


  228. void *find_way(head *hd,int begin,int end,const char *out,int c_num)
  229. {
  230.     
  231.     FILE *fp;
  232.     int i=0;
  233.     int b=0;

  234. ////////////////////////////////////////
  235.     //为构造主路径hopen表的变量声明
  236.     int count=0,sum=1;

  237.     int main=0;
  238.     int main_w[500];


  239.     open *hopen,*op1,*op2,*op3;
  240.     open *hclosed;
  241.     farth *ft,*f;
  242.     head *h;
  243.     node *n;
  244.     /////////////////////////////////////////////
  245.     //为构造备用路径hopen1变量声明
  246.     int x=0,c=0;
  247.     int other_w[500];
  248.     int other=0;
  249.     open *hopen1,*op11,*op21,*op31;
  250.     open *hclosed1;
  251.     farth *ft1,*f1;
  252.     head *h1;
  253.     node *n1;

  254.     ///////////////////////////////////////////////////
  255.     //为构造备用路径hopen2变量声明
  256.     head *hd1;
  257.     node *hn,*hn1;
  258.     open *hopen2,*op12,*op22,*op32;
  259.     open *hclosed2;
  260.     farth *ft2,*f2;
  261.     head *h2;
  262.     node *n2;



  263.     fp=fopen(out,"w");
  264.     if(fp==NULL)
  265.     {
  266.         printf("open %s error\n",out);
  267.         getchar();
  268.         exit(0);
  269.     }

  270.     /////////////////////////////////////////////////////
  271.     //为找出主路径构造hopen表
  272.     hopen=NULL;

  273.     h=(head *)malloc(sizeof(head));
  274.     h->nnext=(node *)malloc(sizeof(node));
  275.     n=(node *)malloc(sizeof(node));


  276.     h=hd->hnext;
  277.     while(1)
  278.     {
  279.         if(h->hnum==end)
  280.         {
  281.             n=h->nnext;
  282.             while(1)
  283.             {
  284.                 if(n->nnext)
  285.                     sum++;
  286.                 else
  287.                     break;
  288.                 n=n->nnext;
  289.             }
  290.             break;
  291.         }
  292.         if(!h->hnext)
  293.         {
  294.             fprintf(fp,"main:\nbackup:\n");
  295.             fclose(fp);
  296.             return;
  297.         }
  298.         h=h->hnext;
  299.     }
  300.     //printf("%d\n",sum);

  301.     h=hd->hnext;

  302.     while(1)
  303.     {
  304.         if(h->hnum==begin)
  305.             break;
  306.         if(!h->hnext)
  307.         {
  308.             fprintf(fp,"main:\nbackup:\n");
  309.             fclose(fp);
  310.             return;
  311.         }
  312.         h=h->hnext;
  313.     }

  314.     
  315.     
  316.     //将初始节点放入open表中
  317.     op1=(open *)malloc(sizeof(open));
  318.     op1->num=h->hnum;
  319.     op1->f=NULL;
  320.     op1->next=NULL;

  321.     hopen=op1;
  322.     hclosed=hopen;

  323.     while(1)
  324.     {
  325.         
  326.     //判定open表是否为空
  327.     if(!hclosed)
  328.     {
  329.         fprintf(fp,"main:\nbackup:\n");
  330.         fclose(fp);
  331.         return;
  332.     }

  333.     if(!h->nnext)
  334.         continue;
  335.     else
  336.     {
  337.         //fprintf(fl,"\n%d :",h->hnum);
  338.         n=h->nnext;
  339.         while(1)
  340.         {
  341.             //fprintf(fl," %d ",n->nnum);
  342.             op2=hopen;
  343.             while(1)
  344.             {
  345.                 
  346.                 if(op2->num==n->nnum)
  347.                 {
  348.                     if(op2->num==end)
  349.                     {
  350.                         count++;
  351.                         //printf("%d \n",h->hnum);
  352.                     }
  353.                     ft=(farth *)malloc(sizeof(farth));
  354.                     ft->num=h->hnum;
  355.                     ft->next=NULL;

  356.                     //f=(farth *)malloc(sizeof(farth));
  357.                     if(!op2->f)
  358.                         op2->f=ft;
  359.                     else
  360.                     {
  361.                         f=op2->f;
  362.                         while(1)
  363.                         {
  364.                             if(!f->next)
  365.                             {
  366.                                 f->next=ft;
  367.                                 break;
  368.                             }
  369.                             f=f->next;
  370.                         }    
  371.                     }
  372.                     b=1;
  373.                     break;
  374.                 }
  375.                 if(!op2->next||b)
  376.                     break;
  377.                 op2=op2->next;

  378.                 //printf("\n");
  379.             }
  380.             if(!b)
  381.             {
  382.                 while(1)
  383.                 {
  384.                     if(op2->num==end)
  385.                     {
  386.                         count++;
  387.                         //printf(" %d \n",h->hnum);
  388.                     }
  389.                     if(!op2->next)
  390.                     {
  391.                         ft=(farth *)malloc(sizeof(farth));
  392.                         ft->num=h->hnum;
  393.                         ft->next=NULL;

  394.                         op3=(open *)malloc(sizeof(open));
  395.                         op3->num=n->nnum;
  396.                         op3->next=NULL;
  397.                         op3->f=ft;

  398.                         op2->next=op3;
  399.                         break;
  400.                     }
  401.                     op2=op2->next;
  402.                 }
  403.             }
  404.             b=0;
  405.             if(!n->nnext)
  406.                 break;
  407.             n=n->nnext;
  408.         }

  409.     }


  410.         if(sum==count)
  411.             break;
  412.         
  413.         //if(hclosed->num!=end)
  414.             hclosed=hclosed->next;
  415.                                          
  416.         n=h->nnext;
  417.         h=hd->hnext;
  418.         while(1)
  419.         {
  420.             if(h->hnum==hclosed->num)
  421.                 break;
  422.             h=h->hnext;
  423.         }
  424.     }//while
  425.     hclosed=hopen;
  426.     while(1)
  427.     {
  428.         if(hclosed->num==end)
  429.             break;
  430.         hclosed=hclosed->next;
  431.     }

  432. //调用main_way找出住路径
  433.     if(hclosed->num==end)
  434.     {
  435.     main_way(hopen,hclosed,begin,end,&main,main_w);
  436.     i=main;
  437.     fprintf(fp,"main:");
  438.     for(i;i>=0;i--)
  439.         fprintf(fp,"%d ",main_w[i]);
  440.     }
  441.     else
  442.     {    
  443.         
  444.         printf("main:backup:\n");
  445.         return;
  446.     }
  447.     //主路径结束
  448.     ///////////////////////////////////////////////////////////////////////

  449.     if(c_num==1)
  450.     {
  451.     /////////////////////////////////////////////////////////////////////////////other_way
  452. //为找出备用路径1构造的hopen1表
  453.     x=main;
  454.     hopen1=NULL;

  455.     //fl=fopen("out.txt","w");

  456.     h1=(head *)malloc(sizeof(head));
  457.     h1->nnext=(node *)malloc(sizeof(node));
  458.     n1=(node *)malloc(sizeof(node));
  459.     //n1->nnext=NULL;
  460.     //n1->nnum=0;

  461.     h1=hd->hnext;

  462.     while(1)
  463.     {
  464.         if(h1->hnum==begin)
  465.             break;
  466.         h1=h1->hnext;
  467.     }

  468.     
  469.     //将初始节点放入open表中
  470.     op11=(open *)malloc(sizeof(open));
  471.     op11->num=h1->hnum;
  472.     op11->f=NULL;
  473.     op11->next=NULL;

  474.     hopen1=op11;
  475.     hclosed1=hopen1;

  476.     while(1)
  477.     {
  478.         
  479.     //判定open表是否为空
  480.     if(!hclosed1)
  481.     {
  482.         fprintf(fp,"\nbackup:\n");
  483.         fclose(fp);
  484.         return;
  485.     }

  486.     //取open表中的第一个放入fclosed中
  487.     //hclosed=hclosed;
  488.     //if(hclosed->num==end)
  489.     //    return;

  490.     if(!h1->nnext)
  491.         continue;
  492.     else
  493.     {
  494.         //fprintf(fl,"\n%d :",h->hnum);
  495.         n1=h1->nnext;
  496.         while(1) //node
  497.         {

  498. //////////////////////////////////
  499.             for(x=main-1;x>0;x--)            //判断main_w中是否已存在该节点
  500.         {
  501.             //printf("%d \n",main_w[x]);
  502.             if(n1->nnum==main_w[x])
  503.             {
  504.                 c=1;
  505.                 break;
  506.             }
  507.         }

  508. ////////////////////////////////////
  509.             if(!c)                            //如果main_w中不存在该节点
  510.             {
  511.             //fprintf(fl," %d ",n->nnum);
  512.             op21=hopen1;
  513.             while(1) //hopen
  514.             {
  515.                 
  516.                 if(op21->num==n1->nnum)
  517.                 {
  518.                     ft1=(farth *)malloc(sizeof(farth));
  519.                     ft1->num=h1->hnum;
  520.                     ft1->next=NULL;

  521.                     //f=(farth *)malloc(sizeof(farth));
  522.                     if(!op21->f)
  523.                         op21->f=ft1;
  524.                     else
  525.                     {
  526.                         f1=op21->f;
  527.                         while(1)
  528.                         {
  529.                             if(!f1->next)
  530.                             {
  531.                                 f1->next=ft1;
  532.                                 break;
  533.                             }
  534.                             f1=f1->next;
  535.                         }    
  536.                     }
  537.                     b=1;
  538.                     break;
  539.                 }
  540.                 if(!op21->next||b)
  541.                     break;
  542.                 op21=op21->next;

  543.                 //printf("\n");
  544.             }
  545.             if(!b)
  546.             {
  547.                 while(1)
  548.                 {
  549.                     
  550.                     if(!op21->next)
  551.                     {
  552.                         ft1=(farth *)malloc(sizeof(farth));
  553.                         ft1->num=h1->hnum;
  554.                         ft1->next=NULL;

  555.                         op31=(open *)malloc(sizeof(open));
  556.                         op31->num=n1->nnum;
  557.                         op31->next=NULL;
  558.                         op31->f=ft1;

  559.                         op21->next=op31;
  560.                         break;
  561.                     }
  562.                     op21=op21->next;
  563.                 }
  564.             }
  565.             }


  566.             c=0;
  567.             b=0;
  568.             x=main;
  569.             if(!n1->nnext)
  570.                 break;
  571.             n1=n1->nnext;
  572.         }

  573.     }//while

  574.         if(hclosed1->num!=end)
  575.         {
  576.             if(!hclosed1->next)
  577.                 break;
  578.             hclosed1=hclosed1->next;
  579.         }
  580.         else
  581.             break;
  582.                                          
  583.         n1=h1->nnext;
  584.         h1=hd->hnext;
  585.         while(1)
  586.         {
  587.             if(h1->hnum==hclosed1->num)
  588.                 break;
  589.             h1=h1->hnext;
  590.         }
  591.     }//else


  592. //调用main_way找出备用路径1
  593.     if(hclosed1->num==end)
  594.     {
  595.     main_way(hopen1,hclosed1,begin,end,&other,other_w);
  596.     i=other;
  597.     fprintf(fp,"\nbackup:");
  598.     for(i;i>=0;i--)
  599.         fprintf(fp,"%d ",other_w[i]);
  600.     }
  601.     else
  602.         fprintf(fp,"\nbackup:\n");


  603.     }                                                    //if(c_num==1)
  604.     ///////////////////////////////////////////////////////////////////////


  605.     if(c_num==2)
  606.     {


  607. //查找备用路径2

  608. //构造备用路径2 邻接表////
  609.     

  610. //    hn=(node *)malloc(sizeof(node));
  611.     //hn->nnext=(node *)malloc(sizeof(node));

  612.     i=main;
  613.     
  614.     while(i>0)                    //循环main_w
  615.     {
  616.         hd1=hd->hnext;
  617.         while(1)
  618.         {
  619.             if(main_w[i]==hd1->hnum)
  620.             {
  621.             
  622.                 hn1=hd1->nnext;
  623.                 hn=hd1->nnext->nnext;
  624.                 if(hd1->nnext->nnum==main_w[i-1])
  625.                 {
  626.                     hd1->nnext=hd1->nnext->nnext;
  627.                     if(hd1->nnext)
  628.                     {    
  629.                         hn1=hd1->nnext;
  630.                         hn=hd1->nnext->nnext;
  631.                     }
  632.                 }
  633.                     
  634.                 else
  635.                     while(1)
  636.                     {
  637.                         if(!hn)
  638.                             break;
  639.                         if(hn->nnum==main_w[i-1])
  640.                         {
  641.                             hn1->nnext=hn->nnext;
  642.                         }
  643.                         
  644.                         hn=hn->nnext;
  645.                     }
  646.             
  647.             }
  648.             if(main_w[i-1]==hd1->hnum)
  649.             {
  650.                 
  651.                 hn1=hd1->nnext;
  652.                 hn=hd1->nnext->nnext;
  653.                 if(hd1->nnext->nnum==main_w[i])
  654.                 {
  655.                     hd1->nnext=hd1->nnext->nnext;
  656.                     if(hd1->nnext)
  657.                     {
  658.                         hn1=hd1->nnext;
  659.                         hn=hd1->nnext->nnext;
  660.                     }
  661.                 }    
  662.                 else
  663.                     while(1)
  664.                     {
  665.                         if(!hn)
  666.                             break;
  667.                         if(hn->nnum==main_w[i])
  668.                         {
  669.                             hn1->nnext=hn->nnext;
  670.                         }
  671.                         
  672.                         hn=hn->nnext;
  673.                     }
  674.             
  675.             }
  676.             if(!hd1->hnext)
  677.                 break;
  678.             hd1=hd1->hnext;
  679.         }
  680.         i--;
  681.     }


  682.     hopen2=NULL;

  683.     h2=(head *)malloc(sizeof(head));
  684.     h2->nnext=(node *)malloc(sizeof(node));
  685.     n2=(node *)malloc(sizeof(node));


  686.     h=hd->hnext;
  687.     while(1)
  688.     {
  689.         if(h->hnum==end)    
  690.             break;
  691.         if(!h->hnext)
  692.         {
  693.             fprintf(fp,"\nbackup:\n");
  694.             fclose(fp);
  695.             return;
  696.         }
  697.         h=h->hnext;
  698.     }

  699.     h2=hd->hnext;

  700.     while(1)
  701.     {
  702.         if(h2->hnum==begin)
  703.             break;
  704.         if(!h2->hnext)
  705.         {
  706.             fprintf(fp,"\nbackup\n");
  707.             return;
  708.         }
  709.         h2=h2->hnext;
  710.     }

  711.     
  712.     
  713.     //将初始节点放入open表中
  714.     op12=(open *)malloc(sizeof(open));
  715.     op12->num=h2->hnum;
  716.     op12->f=NULL;
  717.     op12->next=NULL;

  718.     hopen2=op12;
  719.     hclosed2=hopen2;

  720.     while(1)
  721.     {
  722.         
  723.     //判定open表是否为空
  724.     if(!hclosed2)
  725.     {
  726.         fprintf(fp,"\nbackup:\n");
  727.         fclose(fp);
  728.         return;
  729.     }
  730.     if(!h2->nnext)
  731.         continue;
  732.     else
  733.     {
  734.         //fprintf(fl,"\n%d :",h->hnum);
  735.         n2=h2->nnext;
  736.         while(1)
  737.         {
  738.             //fprintf(fl," %d ",n->nnum);
  739.             op22=hopen2;
  740.             while(1)
  741.             {
  742.                 
  743.                 if(op22->num==n2->nnum)
  744.                 {
  745.                     ft2=(farth *)malloc(sizeof(farth));
  746.                     ft2->num=h2->hnum;
  747.                     ft2->next=NULL;

  748.                     //f=(farth *)malloc(sizeof(farth));
  749.                     if(!op22->f)
  750.                         op22->f=ft2;
  751.                     else
  752.                     {
  753.                         f2=op22->f;
  754.                         while(1)
  755.                         {
  756.                             if(!f2->next)
  757.                             {
  758.                                 f2->next=ft2;
  759.                                 break;
  760.                             }
  761.                             f2=f2->next;
  762.                         }    
  763.                     }
  764.                     b=1;
  765.                     break;
  766.                 }
  767.                 if(!op22->next||b)
  768.                     break;
  769.                 op22=op22->next;

  770.                 //printf("\n");
  771.             }
  772.             if(!b)
  773.             {
  774.                 while(1)
  775.                 {
  776.                     if(!op22->next)
  777.                     {
  778.                         ft2=(farth *)malloc(sizeof(farth));
  779.                         ft2->num=h2->hnum;
  780.                         ft2->next=NULL;

  781.                         op32=(open *)malloc(sizeof(open));
  782.                         op32->num=n2->nnum;
  783.                         op32->next=NULL;
  784.                         op32->f=ft2;

  785.                         op22->next=op32;
  786.                         break;
  787.                     }
  788.                     op22=op22->next;
  789.                 }
  790.             }
  791.             b=0;
  792.             if(!n2->nnext)
  793.                 break;
  794.             n2=n2->nnext;
  795.         }

  796.     }
  797.         if(hclosed2->num!=end)
  798.         {
  799.             if(!hclosed2->next)
  800.                 break;
  801.             hclosed2=hclosed2->next;
  802.         }
  803.         else
  804.             break;
  805.                                          
  806.         n2=h2->nnext;
  807.         h2=hd->hnext;
  808.         while(1)
  809.         {
  810.             if(h2->hnum==hclosed2->num)
  811.                 break;
  812.             h2=h2->hnext;
  813.         }
  814.     }//while
  815.     
  816. //调用main_way找出路径
  817.     if(hclosed2->num==end)
  818.     {
  819.     main_way(hopen2,hclosed2,begin,end,&other,other_w);
  820.     i=other;
  821.     fprintf(fp,"\nbackup:");
  822.     for(i;i>=0;i--)
  823.         fprintf(fp,"%d ",other_w[i]);
  824.     }
  825.     else
  826.         fprintf(fp,"\nbackup:\n");

  827.     }                                                //if(c_num==2)

  828.     //////////////////////////////////////////////////////////
  829.     

  830.     fclose(fp);

  831.     //测试
  832.     /*printf("%d %d ",i++,hclosed->num);

  833.     while(1)
  834.     {
  835.         if(hclosed->num==631)
  836.             break;

  837.         op2=hopen;
  838.         while(1)
  839.         {
  840.             if(op2->num==hclosed->f->num)
  841.             {
  842.                 printf("\n%d %d",i++,op2->num);
  843.                 break;
  844.             }
  845.             op2=op2->next;
  846.         }
  847.         hclosed=op2;
  848.     }*/    //测试结束
  849.     //////////////////////////////////////////////////////

  850.     //return hopen;
  851.     
  852.     
  853. }

  854. void get_way(const char *file,const int begin,int end,int c_num,const char *out)
  855. {
  856.     //printf("%s %s %s \n",argv[0],argv[1],argv[2]);
  857.     open *hopen,*op2,*hclosed;
  858.     farth *fa;


  859.     char ch;
  860.     FILE *fp;
  861.     head *hd,*h;
  862.     node *n;
  863.     char num[20];
  864.     long number=0;
  865.     int i=0,j=0,m=0;
  866.     
  867.     n=(node *)malloc(sizeof(node));
  868.     hd=h=(head *)malloc(sizeof(head));
  869.     hd->hnum=0;
  870.     hd->nnext=NULL;
  871.     hd->hnext=NULL;

  872.     num[0]='\0';
  873.     fp=fopen(file,"r");
  874.     //f=fopen("out.txt","w");
  875.     if(fp==NULL)
  876.     {
  877.         printf("open error\n");
  878.     }
  879.     while(1)
  880.     {
  881.         ch=fgetc(fp);
  882.         if(ch!=','&&ch!=10&&ch!=-1)
  883.         {
  884.             num[i]=ch;
  885.             num[i+1]='\0';
  886.             i++;
  887.         }
  888.         else
  889.         {
  890.             
  891.             number=change(num);
  892.             num[0]='\0';
  893.             i=0;
  894.             fun3(hd,number);
  895.             //fprintf(f,"%d\n",number);
  896.         }
  897.         if(ch==-1)
  898.             break;        
  899.     }
  900.     

  901.     find_way(hd,begin,end,out,c_num);
  902.     
  903.     /*h=hd->hnext;//用于输出测试 将制作的邻接表输出到out.txt中
  904.     while(1)
  905.     {
  906.         fprintf(f,"%d: ",h->hnum);
  907.         n=h->nnext;
  908.         while(n)
  909.         {
  910.             fprintf(f,"%d ",n->nnum);
  911.             n=n->nnext;
  912.         }
  913.         fprintf(f,"\n");
  914.         if(!h->hnext)
  915.             break;
  916.         h=h->hnext;
  917.     }*/            //测试结束

  918.     //hclosed=(open *)malloc(sizeof(open));
  919. ///////////////////////////////////
  920.     
  921.     //fclose(f);
  922.     
  923.     
  924.     fclose(fp);
  925. }


点击(此处)折叠或打开

  1. //路径搜索 自定义头文件
  2. //2013.12
  3. //santhinking
  4. //ecit


  5. #ifndef myhead
  6. #define myhead

  7. extern void get_way(char *file,int begin,int end,int c_num,char *out);
  8. extern int change(char *ch);


  9. #endif
makefile文件:

点击(此处)折叠或打开

  1. ####################makefile###############
  2. #time:2013.12
  3. #author:santhinking
  4. #addr:ecit

  5. demo:main.c test.c myhead.h
  6.     gcc $^ -o $@




阅读(80) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~