我采用的是:广度优先搜索
已知图G=(V,E)和一个源顶点s,宽度优先搜索以一种系统的方式探寻G的边,从而“发现”s所能到达的所有顶点,并计算s到所有这些顶点的距离(最少边数),该算法同时能生成一棵根为s且包括所有可达顶点的宽度优先树。对从s可达的任意顶点v,宽度优先树中从s到v的路径对应于图G中从s到v的最短路径,即包含最小边数的路径。该算法对有向图和无向图同样适用。
之所以称之为宽度优先算法,是因为算法自始至终一直通过已找到和未找到顶点之间的边界向外扩展,就是说,算法首先搜索和s距离为k的所有顶点,然后再去搜索和S距离为k+l的其他顶点。
为了保持搜索的轨迹,宽度优先搜索为每个顶点着色:白色、灰色或黑色。算法开始前所有顶点都是白色,随着搜索的进行,各顶点会逐渐变成灰色,然后成为黑色。在搜索中第一次碰到一顶点时,我们说该顶点被发现,此时该顶点变为非白色顶点。因此,灰色和黑色顶点都已被发现,但是,宽度优先搜索算法对它们加以区分以保证搜索以宽度优先的方式执行。若(u,v)∈E且顶点u为黑色,那么顶点v要么是灰色,要么是黑色,就是说,所有和黑色顶点邻接的顶点都已被发现。灰色顶点可以与一些白色顶点相邻接,它们代表着已找到和未找到顶点之间的边界。
在宽度优先搜索过程中建立了一棵宽度优先树,起始时只包含根节点,即源顶点s.在扫描已发现顶点u的邻接表的过程中每发现一个白色顶点v,该顶点v及边(u,v)就被添加到树中。在宽度优先树中,我们称结点u 是结点v的先辈或父母结点。因为一个结点至多只能被发现一次,因此它最多只能有--个父母结点。相对根结点来说祖先和后裔关系的定义和通常一样:如果u处于树中从根s到结点v的路径中,那么u称为v的祖先,v是u的后裔。
main.c:
-
//路径搜索 程序入口
-
//2013.12
-
//santhinking
-
//ecit
-
-
-
#include<stdio.h>
-
#include<stdlib.h>
-
#include<string.h>
-
#include"myhead.h"
-
-
void get_msg(const char *str,char *file,int *begin,int *end,int *c_num,char *out);
-
-
//int change_num(char *s);
-
-
-
int main(int argc,char *argv[])
-
{
-
char file[30];
-
char out[30];
-
int begin,end;
-
int c_num;
-
-
//char str[]="/fBigdata.txt/s91/d93/c2/oout.txt";
-
//printf("%s\n",argv[1]);
-
-
if(argc!=2)
-
{
-
printf("usage:demo.exe /fBigdata.txt/s145/d258/c1/oout.txt\n");
-
exit(0);
-
}
-
get_msg(argv[1],file,&begin,&end,&c_num,out);
-
//get_msg(str,file,&begin,&end,&c_num,out);
-
-
get_way(file,begin,end,c_num,out);
-
-
return 0;
-
}
-
-
void get_msg(const char *str,char *file,int *begin,int *end,int *c_num,char *out)
-
{
-
char msg[5][30];
-
int i=0;
-
int n=0;
-
int j=0;
-
-
//printf("%d\n",strlen(str));
-
-
for(i;i<strlen(str);i++)
-
{
-
if(str[i]=='/'&&str[i+1]=='f')
-
{
-
j=0;
-
for(n=i+2;n<strlen(str);n++)
-
{
-
if(str[n]=='/'||str[n]=='\0')
-
break;
-
file[j]=str[n];
-
j++;
-
}
-
file[j]='\0';
-
}
-
if(str[i]=='/'&&str[i+1]=='s')
-
{
-
j=0;
-
for(n=i+2;n<strlen(str);n++)
-
{
-
if(str[n]=='/'||str[n]=='\0')
-
break;
-
msg[1][j]=str[n];
-
j++;
-
}
-
msg[1][j]='\0';
-
}
-
if(str[i]=='/'&&str[i+1]=='d')
-
{
-
j=0;
-
for(n=i+2;n<strlen(str);n++)
-
{
-
if(str[n]=='/'||str[n]=='\0')
-
break;
-
msg[2][j]=str[n];
-
j++;
-
}
-
msg[2][j]='\0';
-
}
-
if(str[i]=='/'&&str[i+1]=='c')
-
{
-
j=0;
-
for(n=i+2;n<strlen(str);n++)
-
{
-
if(str[n]=='/'||str[n]=='\0')
-
break;
-
msg[3][j]=str[n];
-
j++;
-
}
-
msg[3][j]='\0';
-
}
-
if(str[i]=='/'&&str[i+1]=='o')
-
{
-
j=0;
-
for(n=i+2;n<strlen(str);n++)
-
{
-
if(str[n]=='/'||str[n]=='\0')
-
break;
-
out[j]=str[n];
-
j++;
-
}
-
out[j]='\0';
-
}
-
}
-
*begin=change(msg[1]);
-
*end=change(msg[2]);
-
*c_num=change(msg[3]);
-
}
-
-
/*int change_num(char *s)
-
{
-
int num=0;
-
int i;
-
for(i=0;i<strlen(s);i++)
-
{
-
if(s[i]<'0'||s[i]>'9')
-
continue;
-
else
-
{
-
num=num*10;
-
switch(s[i])
-
{
-
case '0':num+=0;
-
break;
-
case '1':num+=1;
-
break;
-
case '2':num+=2;
-
break;
-
case '3':num+=3;
-
break;
-
case '4':num+=4;
-
break;
-
case '5':num+=5;
-
break;
-
case '6':num+=6;
-
break;
-
case '7':num+=7;
-
break;
-
case '8':num+=8;
-
break;
-
case '9':num+=9;
-
break;
-
}
-
}
-
}
-
return num;
-
}*/
test.c:
-
//路径搜索 主算法实现
-
//2013.12
-
//santhinking
-
//ecit
-
//关于备用路径c2算法有问题
-
-
#include <stdio.h>
-
#include <math.h>
-
#include <string.h>
-
#include <stdlib.h>
-
//#include "struct_data.h"
-
-
-
//邻接表数据结构 定义
-
typedef struct node{ //节点结构
-
int nnum; //节点数据
-
struct node *nnext; //下一节点指针
-
}node;
-
-
typedef struct head{ //头结构
-
int hnum; //节点数据
-
struct head *hnext; //头节点指针
-
struct node *nnext; //指向节点指针
-
}head;
-
//邻接表数据定义 结束
-
-
//定义open表结构
-
-
typedef struct farth{ //存储父节点数据结构
-
int num;
-
struct farth *next;
-
}farth;
-
-
typedef struct open{
-
int num; //节点数据
-
struct farth *f;
-
struct open *next;
-
}open;
-
-
//定义open表结束
-
-
void fun2(char num[],char ch)
-
{
-
int i=strlen(num);
-
//printf("%d %d\n",strlen(num),sizeof(num));
-
-
if(strlen(num)<sizeof(num))
-
{
-
num[i]=ch;
-
num[i+1]='\0';
-
}
-
-
//return num;
-
}
-
-
int change(char *ch)
-
{
-
int num=0;
-
int i;
-
for(i=0;i<strlen(ch);i++)
-
{
-
if(ch[i]<'0'||ch[i]>'9')
-
continue;
-
else
-
{
-
num=num*10;
-
switch(ch[i])
-
{
-
case '0':num+=0;
-
break;
-
case '1':num+=1;
-
break;
-
case '2':num+=2;
-
break;
-
case '3':num+=3;
-
break;
-
case '4':num+=4;
-
break;
-
case '5':num+=5;
-
break;
-
case '6':num+=6;
-
break;
-
case '7':num+=7;
-
break;
-
case '8':num+=8;
-
break;
-
case '9':num+=9;
-
break;
-
}
-
}
-
}
-
return num;
-
}
-
-
void add_num(head *hd,int num1,int num2)
-
{
-
int a,b;
-
-
head *h1;
-
//static int count=0;
-
head *h2,*h3;
-
-
node *n1,*n2,*n3;
-
n1=(node *)malloc(sizeof(node));
-
h1=(head *)malloc(sizeof(head));
-
a=b=0;
-
h1=hd;
-
h1->nnext=(node *)malloc(sizeof(node));
-
h1->nnext->nnum=0;
-
h1->nnext->nnext=NULL;
-
-
//if(count%2==0)
-
while(1)
-
{
-
if(h1->hnum==num1)
-
{
-
a=1;
-
n1=h1->nnext;
-
while(n1->nnext)
-
{
-
n1=n1->nnext;
-
}
-
if(!(n1->nnext))
-
{
-
n2=(node *)malloc(sizeof(node));
-
n2->nnum=num2;
-
n2->nnext=NULL;
-
-
n1->nnext=n2;
-
//n1->nnext=NULL;
-
//n1->nnum=num2;
-
n1=n1->nnext;
-
//free(n2);
-
}
-
}
-
if(h1->hnum==num2)
-
{
-
b=1;
-
n1=h1->nnext;
-
while(n1->nnext)
-
{
-
n1=n1->nnext;
-
}
-
if(!(n1->nnext))
-
{
-
n3=(node *)malloc(sizeof(node));
-
n3->nnum=num1;
-
n3->nnext=NULL;
-
-
n1->nnext=n3;
-
//n1->nnext=NULL;
-
//n1->nnum=num1;
-
n1=n1->nnext;
-
//free(n2);
-
}
-
}
-
if(a&&b)
-
break;
-
if(!h1->hnext)
-
break;
-
h1=h1->hnext;
-
}
-
if(a==0||b==0)
-
{
-
while(h1->hnext)
-
h1=h1->hnext;
-
if(!a)
-
{
-
h2=(head *)malloc(sizeof(head));
-
h2->hnum=num1;
-
h2->hnext=NULL;
-
h2->nnext=NULL;
-
h2->nnext=(node *)malloc(sizeof(node));
-
h2->nnext->nnext=NULL;
-
h2->nnext->nnum=0;
-
-
h1->hnext=h2;
-
-
//free(h2);
-
-
h2->nnext->nnum=num2;
-
-
h1=h1->hnext;
-
}
-
if(!b)
-
{
-
//h1=h1->hnext;
-
h3=(head *)malloc(sizeof(head));
-
h3->hnum=num2;
-
h3->hnext=NULL;
-
h3->nnext=NULL;
-
h3->nnext=(node *)malloc(sizeof(node));
-
h3->nnext->nnum=0;
-
h3->nnext->nnext=NULL;
-
-
h1->hnext=h3;
-
-
//free(h3);
-
h3->nnext->nnum=num1;
-
-
-
h1=h1->hnext;
-
}
-
}
-
-
}
-
-
-
void fun3(head *hd,int num)
-
{
-
static int count=0;
-
static int num1;
-
if(count%2==0)
-
num1=num;
-
if(count%2==1)
-
add_num(hd,num1,num);
-
-
count ++;
-
}
-
-
-
int * main_way(open *hopen,open *hclosed,int begin,int end,int *main,int *main_way)
-
{
-
int i=0;
-
open *op2;
-
-
//main_way=(int *)malloc(500*sizeof(int));
-
-
main_way[i]=hclosed->num;
-
i++;
-
//printf("%d %d ",i++,hclosed->num);
-
-
-
while(1)
-
{
-
if(hclosed->num==begin)
-
break;
-
-
op2=hopen;
-
while(1)
-
{
-
if(op2->num==hclosed->f->num)
-
{
-
main_way[i]=op2->num;
-
i++;
-
//printf("\n%d %d",i++,op2->num);
-
break;
-
}
-
op2=op2->next;
-
}
-
hclosed=op2;
-
}
-
i--;
-
*main=i;
-
-
return main_way;
-
}
-
-
-
void *find_way(head *hd,int begin,int end,const char *out,int c_num)
-
{
-
-
FILE *fp;
-
int i=0;
-
int b=0;
-
-
////////////////////////////////////////
-
//为构造主路径hopen表的变量声明
-
int count=0,sum=1;
-
-
int main=0;
-
int main_w[500];
-
-
-
open *hopen,*op1,*op2,*op3;
-
open *hclosed;
-
farth *ft,*f;
-
head *h;
-
node *n;
-
/////////////////////////////////////////////
-
//为构造备用路径hopen1变量声明
-
int x=0,c=0;
-
int other_w[500];
-
int other=0;
-
open *hopen1,*op11,*op21,*op31;
-
open *hclosed1;
-
farth *ft1,*f1;
-
head *h1;
-
node *n1;
-
-
///////////////////////////////////////////////////
-
//为构造备用路径hopen2变量声明
-
head *hd1;
-
node *hn,*hn1;
-
open *hopen2,*op12,*op22,*op32;
-
open *hclosed2;
-
farth *ft2,*f2;
-
head *h2;
-
node *n2;
-
-
-
-
fp=fopen(out,"w");
-
if(fp==NULL)
-
{
-
printf("open %s error\n",out);
-
getchar();
-
exit(0);
-
}
-
-
/////////////////////////////////////////////////////
-
//为找出主路径构造hopen表
-
hopen=NULL;
-
-
h=(head *)malloc(sizeof(head));
-
h->nnext=(node *)malloc(sizeof(node));
-
n=(node *)malloc(sizeof(node));
-
-
-
h=hd->hnext;
-
while(1)
-
{
-
if(h->hnum==end)
-
{
-
n=h->nnext;
-
while(1)
-
{
-
if(n->nnext)
-
sum++;
-
else
-
break;
-
n=n->nnext;
-
}
-
break;
-
}
-
if(!h->hnext)
-
{
-
fprintf(fp,"main:\nbackup:\n");
-
fclose(fp);
-
return;
-
}
-
h=h->hnext;
-
}
-
//printf("%d\n",sum);
-
-
h=hd->hnext;
-
-
while(1)
-
{
-
if(h->hnum==begin)
-
break;
-
if(!h->hnext)
-
{
-
fprintf(fp,"main:\nbackup:\n");
-
fclose(fp);
-
return;
-
}
-
h=h->hnext;
-
}
-
-
-
-
//将初始节点放入open表中
-
op1=(open *)malloc(sizeof(open));
-
op1->num=h->hnum;
-
op1->f=NULL;
-
op1->next=NULL;
-
-
hopen=op1;
-
hclosed=hopen;
-
-
while(1)
-
{
-
-
//判定open表是否为空
-
if(!hclosed)
-
{
-
fprintf(fp,"main:\nbackup:\n");
-
fclose(fp);
-
return;
-
}
-
-
if(!h->nnext)
-
continue;
-
else
-
{
-
//fprintf(fl,"\n%d :",h->hnum);
-
n=h->nnext;
-
while(1)
-
{
-
//fprintf(fl," %d ",n->nnum);
-
op2=hopen;
-
while(1)
-
{
-
-
if(op2->num==n->nnum)
-
{
-
if(op2->num==end)
-
{
-
count++;
-
//printf("%d \n",h->hnum);
-
}
-
ft=(farth *)malloc(sizeof(farth));
-
ft->num=h->hnum;
-
ft->next=NULL;
-
-
//f=(farth *)malloc(sizeof(farth));
-
if(!op2->f)
-
op2->f=ft;
-
else
-
{
-
f=op2->f;
-
while(1)
-
{
-
if(!f->next)
-
{
-
f->next=ft;
-
break;
-
}
-
f=f->next;
-
}
-
}
-
b=1;
-
break;
-
}
-
if(!op2->next||b)
-
break;
-
op2=op2->next;
-
-
//printf("\n");
-
}
-
if(!b)
-
{
-
while(1)
-
{
-
if(op2->num==end)
-
{
-
count++;
-
//printf(" %d \n",h->hnum);
-
}
-
if(!op2->next)
-
{
-
ft=(farth *)malloc(sizeof(farth));
-
ft->num=h->hnum;
-
ft->next=NULL;
-
-
op3=(open *)malloc(sizeof(open));
-
op3->num=n->nnum;
-
op3->next=NULL;
-
op3->f=ft;
-
-
op2->next=op3;
-
break;
-
}
-
op2=op2->next;
-
}
-
}
-
b=0;
-
if(!n->nnext)
-
break;
-
n=n->nnext;
-
}
-
-
}
-
-
-
if(sum==count)
-
break;
-
-
//if(hclosed->num!=end)
-
hclosed=hclosed->next;
-
-
n=h->nnext;
-
h=hd->hnext;
-
while(1)
-
{
-
if(h->hnum==hclosed->num)
-
break;
-
h=h->hnext;
-
}
-
}//while
-
hclosed=hopen;
-
while(1)
-
{
-
if(hclosed->num==end)
-
break;
-
hclosed=hclosed->next;
-
}
-
-
//调用main_way找出住路径
-
if(hclosed->num==end)
-
{
-
main_way(hopen,hclosed,begin,end,&main,main_w);
-
i=main;
-
fprintf(fp,"main:");
-
for(i;i>=0;i--)
-
fprintf(fp,"%d ",main_w[i]);
-
}
-
else
-
{
-
-
printf("main:backup:\n");
-
return;
-
}
-
//主路径结束
-
///////////////////////////////////////////////////////////////////////
-
-
if(c_num==1)
-
{
-
/////////////////////////////////////////////////////////////////////////////other_way
-
//为找出备用路径1构造的hopen1表
-
x=main;
-
hopen1=NULL;
-
-
//fl=fopen("out.txt","w");
-
-
h1=(head *)malloc(sizeof(head));
-
h1->nnext=(node *)malloc(sizeof(node));
-
n1=(node *)malloc(sizeof(node));
-
//n1->nnext=NULL;
-
//n1->nnum=0;
-
-
h1=hd->hnext;
-
-
while(1)
-
{
-
if(h1->hnum==begin)
-
break;
-
h1=h1->hnext;
-
}
-
-
-
//将初始节点放入open表中
-
op11=(open *)malloc(sizeof(open));
-
op11->num=h1->hnum;
-
op11->f=NULL;
-
op11->next=NULL;
-
-
hopen1=op11;
-
hclosed1=hopen1;
-
-
while(1)
-
{
-
-
//判定open表是否为空
-
if(!hclosed1)
-
{
-
fprintf(fp,"\nbackup:\n");
-
fclose(fp);
-
return;
-
}
-
-
//取open表中的第一个放入fclosed中
-
//hclosed=hclosed;
-
//if(hclosed->num==end)
-
// return;
-
-
if(!h1->nnext)
-
continue;
-
else
-
{
-
//fprintf(fl,"\n%d :",h->hnum);
-
n1=h1->nnext;
-
while(1) //node
-
{
-
-
//////////////////////////////////
-
for(x=main-1;x>0;x--) //判断main_w中是否已存在该节点
-
{
-
//printf("%d \n",main_w[x]);
-
if(n1->nnum==main_w[x])
-
{
-
c=1;
-
break;
-
}
-
}
-
-
////////////////////////////////////
-
if(!c) //如果main_w中不存在该节点
-
{
-
//fprintf(fl," %d ",n->nnum);
-
op21=hopen1;
-
while(1) //hopen
-
{
-
-
if(op21->num==n1->nnum)
-
{
-
ft1=(farth *)malloc(sizeof(farth));
-
ft1->num=h1->hnum;
-
ft1->next=NULL;
-
-
//f=(farth *)malloc(sizeof(farth));
-
if(!op21->f)
-
op21->f=ft1;
-
else
-
{
-
f1=op21->f;
-
while(1)
-
{
-
if(!f1->next)
-
{
-
f1->next=ft1;
-
break;
-
}
-
f1=f1->next;
-
}
-
}
-
b=1;
-
break;
-
}
-
if(!op21->next||b)
-
break;
-
op21=op21->next;
-
-
//printf("\n");
-
}
-
if(!b)
-
{
-
while(1)
-
{
-
-
if(!op21->next)
-
{
-
ft1=(farth *)malloc(sizeof(farth));
-
ft1->num=h1->hnum;
-
ft1->next=NULL;
-
-
op31=(open *)malloc(sizeof(open));
-
op31->num=n1->nnum;
-
op31->next=NULL;
-
op31->f=ft1;
-
-
op21->next=op31;
-
break;
-
}
-
op21=op21->next;
-
}
-
}
-
}
-
-
-
c=0;
-
b=0;
-
x=main;
-
if(!n1->nnext)
-
break;
-
n1=n1->nnext;
-
}
-
-
}//while
-
-
if(hclosed1->num!=end)
-
{
-
if(!hclosed1->next)
-
break;
-
hclosed1=hclosed1->next;
-
}
-
else
-
break;
-
-
n1=h1->nnext;
-
h1=hd->hnext;
-
while(1)
-
{
-
if(h1->hnum==hclosed1->num)
-
break;
-
h1=h1->hnext;
-
}
-
}//else
-
-
-
//调用main_way找出备用路径1
-
if(hclosed1->num==end)
-
{
-
main_way(hopen1,hclosed1,begin,end,&other,other_w);
-
i=other;
-
fprintf(fp,"\nbackup:");
-
for(i;i>=0;i--)
-
fprintf(fp,"%d ",other_w[i]);
-
}
-
else
-
fprintf(fp,"\nbackup:\n");
-
-
-
} //if(c_num==1)
-
///////////////////////////////////////////////////////////////////////
-
-
-
if(c_num==2)
-
{
-
-
-
//查找备用路径2
-
-
//构造备用路径2 邻接表////
-
-
-
// hn=(node *)malloc(sizeof(node));
-
//hn->nnext=(node *)malloc(sizeof(node));
-
-
i=main;
-
-
while(i>0) //循环main_w
-
{
-
hd1=hd->hnext;
-
while(1)
-
{
-
if(main_w[i]==hd1->hnum)
-
{
-
-
hn1=hd1->nnext;
-
hn=hd1->nnext->nnext;
-
if(hd1->nnext->nnum==main_w[i-1])
-
{
-
hd1->nnext=hd1->nnext->nnext;
-
if(hd1->nnext)
-
{
-
hn1=hd1->nnext;
-
hn=hd1->nnext->nnext;
-
}
-
}
-
-
else
-
while(1)
-
{
-
if(!hn)
-
break;
-
if(hn->nnum==main_w[i-1])
-
{
-
hn1->nnext=hn->nnext;
-
}
-
-
hn=hn->nnext;
-
}
-
-
}
-
if(main_w[i-1]==hd1->hnum)
-
{
-
-
hn1=hd1->nnext;
-
hn=hd1->nnext->nnext;
-
if(hd1->nnext->nnum==main_w[i])
-
{
-
hd1->nnext=hd1->nnext->nnext;
-
if(hd1->nnext)
-
{
-
hn1=hd1->nnext;
-
hn=hd1->nnext->nnext;
-
}
-
}
-
else
-
while(1)
-
{
-
if(!hn)
-
break;
-
if(hn->nnum==main_w[i])
-
{
-
hn1->nnext=hn->nnext;
-
}
-
-
hn=hn->nnext;
-
}
-
-
}
-
if(!hd1->hnext)
-
break;
-
hd1=hd1->hnext;
-
}
-
i--;
-
}
-
-
-
hopen2=NULL;
-
-
h2=(head *)malloc(sizeof(head));
-
h2->nnext=(node *)malloc(sizeof(node));
-
n2=(node *)malloc(sizeof(node));
-
-
-
h=hd->hnext;
-
while(1)
-
{
-
if(h->hnum==end)
-
break;
-
if(!h->hnext)
-
{
-
fprintf(fp,"\nbackup:\n");
-
fclose(fp);
-
return;
-
}
-
h=h->hnext;
-
}
-
-
h2=hd->hnext;
-
-
while(1)
-
{
-
if(h2->hnum==begin)
-
break;
-
if(!h2->hnext)
-
{
-
fprintf(fp,"\nbackup\n");
-
return;
-
}
-
h2=h2->hnext;
-
}
-
-
-
-
//将初始节点放入open表中
-
op12=(open *)malloc(sizeof(open));
-
op12->num=h2->hnum;
-
op12->f=NULL;
-
op12->next=NULL;
-
-
hopen2=op12;
-
hclosed2=hopen2;
-
-
while(1)
-
{
-
-
//判定open表是否为空
-
if(!hclosed2)
-
{
-
fprintf(fp,"\nbackup:\n");
-
fclose(fp);
-
return;
-
}
-
if(!h2->nnext)
-
continue;
-
else
-
{
-
//fprintf(fl,"\n%d :",h->hnum);
-
n2=h2->nnext;
-
while(1)
-
{
-
//fprintf(fl," %d ",n->nnum);
-
op22=hopen2;
-
while(1)
-
{
-
-
if(op22->num==n2->nnum)
-
{
-
ft2=(farth *)malloc(sizeof(farth));
-
ft2->num=h2->hnum;
-
ft2->next=NULL;
-
-
//f=(farth *)malloc(sizeof(farth));
-
if(!op22->f)
-
op22->f=ft2;
-
else
-
{
-
f2=op22->f;
-
while(1)
-
{
-
if(!f2->next)
-
{
-
f2->next=ft2;
-
break;
-
}
-
f2=f2->next;
-
}
-
}
-
b=1;
-
break;
-
}
-
if(!op22->next||b)
-
break;
-
op22=op22->next;
-
-
//printf("\n");
-
}
-
if(!b)
-
{
-
while(1)
-
{
-
if(!op22->next)
-
{
-
ft2=(farth *)malloc(sizeof(farth));
-
ft2->num=h2->hnum;
-
ft2->next=NULL;
-
-
op32=(open *)malloc(sizeof(open));
-
op32->num=n2->nnum;
-
op32->next=NULL;
-
op32->f=ft2;
-
-
op22->next=op32;
-
break;
-
}
-
op22=op22->next;
-
}
-
}
-
b=0;
-
if(!n2->nnext)
-
break;
-
n2=n2->nnext;
-
}
-
-
}
-
if(hclosed2->num!=end)
-
{
-
if(!hclosed2->next)
-
break;
-
hclosed2=hclosed2->next;
-
}
-
else
-
break;
-
-
n2=h2->nnext;
-
h2=hd->hnext;
-
while(1)
-
{
-
if(h2->hnum==hclosed2->num)
-
break;
-
h2=h2->hnext;
-
}
-
}//while
-
-
//调用main_way找出路径
-
if(hclosed2->num==end)
-
{
-
main_way(hopen2,hclosed2,begin,end,&other,other_w);
-
i=other;
-
fprintf(fp,"\nbackup:");
-
for(i;i>=0;i--)
-
fprintf(fp,"%d ",other_w[i]);
-
}
-
else
-
fprintf(fp,"\nbackup:\n");
-
-
} //if(c_num==2)
-
-
//////////////////////////////////////////////////////////
-
-
-
fclose(fp);
-
-
//测试
-
/*printf("%d %d ",i++,hclosed->num);
-
-
while(1)
-
{
-
if(hclosed->num==631)
-
break;
-
-
op2=hopen;
-
while(1)
-
{
-
if(op2->num==hclosed->f->num)
-
{
-
printf("\n%d %d",i++,op2->num);
-
break;
-
}
-
op2=op2->next;
-
}
-
hclosed=op2;
-
}*/ //测试结束
-
//////////////////////////////////////////////////////
-
-
//return hopen;
-
-
-
}
-
-
void get_way(const char *file,const int begin,int end,int c_num,const char *out)
-
{
-
//printf("%s %s %s \n",argv[0],argv[1],argv[2]);
-
open *hopen,*op2,*hclosed;
-
farth *fa;
-
-
-
char ch;
-
FILE *fp;
-
head *hd,*h;
-
node *n;
-
char num[20];
-
long number=0;
-
int i=0,j=0,m=0;
-
-
n=(node *)malloc(sizeof(node));
-
hd=h=(head *)malloc(sizeof(head));
-
hd->hnum=0;
-
hd->nnext=NULL;
-
hd->hnext=NULL;
-
-
num[0]='\0';
-
fp=fopen(file,"r");
-
//f=fopen("out.txt","w");
-
if(fp==NULL)
-
{
-
printf("open error\n");
-
}
-
while(1)
-
{
-
ch=fgetc(fp);
-
if(ch!=','&&ch!=10&&ch!=-1)
-
{
-
num[i]=ch;
-
num[i+1]='\0';
-
i++;
-
}
-
else
-
{
-
-
number=change(num);
-
num[0]='\0';
-
i=0;
-
fun3(hd,number);
-
//fprintf(f,"%d\n",number);
-
}
-
if(ch==-1)
-
break;
-
}
-
-
-
find_way(hd,begin,end,out,c_num);
-
-
/*h=hd->hnext;//用于输出测试 将制作的邻接表输出到out.txt中
-
while(1)
-
{
-
fprintf(f,"%d: ",h->hnum);
-
n=h->nnext;
-
while(n)
-
{
-
fprintf(f,"%d ",n->nnum);
-
n=n->nnext;
-
}
-
fprintf(f,"\n");
-
if(!h->hnext)
-
break;
-
h=h->hnext;
-
}*/ //测试结束
-
-
//hclosed=(open *)malloc(sizeof(open));
-
///////////////////////////////////
-
-
//fclose(f);
-
-
-
fclose(fp);
-
}
-
//路径搜索 自定义头文件
-
//2013.12
-
//santhinking
-
//ecit
-
-
-
#ifndef myhead
-
#define myhead
-
-
extern void get_way(char *file,int begin,int end,int c_num,char *out);
-
extern int change(char *ch);
-
-
-
#endif
makefile文件:
-
####################makefile###############
-
#time:2013.12
-
#author:santhinking
-
#addr:ecit
-
-
demo:main.c test.c myhead.h
-
gcc $^ -o $@