练习写的代码,帮我瞧瞧吧~
几个月前学C语言时编的,主函数里有一个矩阵表示的有向连通图,功能是输入起止位置,找出所有最短路径。因为还没学过数据结构,感觉方法应该很笨拙吧,呵呵。晒晒代码高手请给看看问题在哪,关于啥都行!
中文有乱码,我把源文件也传上来吧
程序代码:
#include "stdio.h" #define SquSize 20 #define MaxLen 1000 #define TRUE 1 #define FALSE 0 int stack[MaxLen];//全局变量 int sp=-1; int tmp[MaxLen]; int tsp=-1; typedef struct { int data[MaxLen]; int key [MaxLen/2]; int locate; }Line; Line *Init(Line *p)//初始化 { p=(Line * )malloc(sizeof(Line)); p->locate=-2; return p; } int Check(Line *r,int i,int j) { int s,a,b; if(r->locate<0)//栈为空 return 1; for(s=0;s<=(r->locate);s+=2) { a=r->data[s]; b=r->data[s+1]; if(a==i&&b==j) return 0; } return 1; } void Add(Line *s,int i,int j,int k)//添加序偶 { s->locate+=2; s->data[s->locate]=i; s->data[s->locate+1]=j; s->key[s->locate/2]=k; } void AddDire(Line *s,int (*r)[SquSize]) { int i,j; for(i=0;i<SquSize;i++) for(j=0;j<SquSize;j++) { if(i!=j&&*(*(r+i)+j)==1) { s->locate+=2; s->data[s->locate]=i; s->data[s->locate+1]=j; s->key[s->locate/2]=-1; } } } int Comb(int(*p)[SquSize],int(*q)[SquSize],int(*r)[SquSize],int i,int j,Line *datbox) { int k,tmp1,tmp2,find=0,count=0; for(k=0;k<SquSize;k++) { tmp1=*(*(p+i)+k); tmp2=*(*(q+k)+j); if(tmp1*tmp2) { count=1; *(*(r+i)+j)=1; Add(datbox,i,j,k); find=1; } } if(find==0) *(*(r+i)+j)=0; return count;//count为零表示没有点可插入,终止调用Calculate() } int Calculate(int(*p)[SquSize],int(*q)[SquSize],int (*r)[SquSize],Line *datbox) { int i,j,count=0; for(i=0;i<SquSize;i++) for(j=0;j<SquSize;j++) { if(i==j)//½«r[i][j]Ö±½ÓÖÃÁã *(*(r+i)+j)=0; else { if(Check(datbox,i,j)) { count+=Comb(p,q,r,i,j,datbox); } else *(*(r+i)+j)=0; } } return count; } int Search(Line *s,int i,int j) { int a; int k,path=1,find=FALSE; if(s->locate<0) { printf("矩阵为空!"); return 0; } if(i==j) { printf("起止位置重合\n"); return 0; } for(k=0;k<=(s->locate);k+=2) { if(s->data[k]==i&&s->data[k+1]==j) find=TRUE; } if(find) { printf("寻找到下列路径\n"); PathOut(s,i,j ); } else printf("未找到可达路径\n"); return 0; } int Find(int *p,Line *s,int i,int j) { int m,n,newkey; for(m=0;m<=s->locate;m+=2) { if(s->data[m]==i&&s->data[m+1]==j) { newkey=TRUE; for(n=0;n<=tsp;n++) { if(tmp[n]==s->key[m/2]) newkey=FALSE; } if(newkey) { *p=s->key[m/2]; return 1; } } } return 0; } int PathOut(Line *s,int i,int j) { int find,key,op,tp,move; int *p=&key; int si=i; sp++; stack[sp]=si; while(1) { find=Find(p,s,i,j); if(find) { if(key!=-1) { sp++; stack[sp]=key; tsp++; tmp[tsp]=key; i=key; } else { for(op=0;op<=sp;op++) printf("%d->",stack[op]); printf("%d\n",j); if(i==si) { sp=-1; tsp=-1; return 0; } sp--; i=stack[sp]; } } else { if(i==si) { sp=-1; tsp=-1; return 0; } for(tp=0;tp<=tsp;tp++) { if(i==tmp[tp]) move=tp; } tsp=move; sp--; i=stack[sp]; } } } int main() { /***********************初始化****************************/ int i,j,count=1,set,arrive; Line *datbox; datbox=Init(datbox); int (*p)[SquSize],(*q)[SquSize],(*r)[SquSize],(*exchg)[SquSize]; /*0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9*/ int sq1[SquSize][SquSize]={{0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//0 {0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//1 {0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0},//2 {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},//3 {0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//4 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},//5 {0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},//6 {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},//7 {0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},//8 {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//9 {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//0 {0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,1,0,0,0},//1 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//2 {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},//3 {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1},//4 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},//5 {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0},//6 {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},//7 {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},//8 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0}};//9 int sq2[SquSize][SquSize]; for(i=0;i<SquSize;i++) for(j=0;j<SquSize;j++) sq2[i][j]=sq1[i][j]; int sq3[SquSize][SquSize]; for(i=0;i<SquSize;i++) for(j=0;j<SquSize;j++) sq3[i][j]=0; p=sq1; q=sq2; r=sq3; /*********************计算部分***************************/ AddDire(datbox,p); while(count) { count=Calculate(p,q,r,datbox); exchg=q; q=r; r=exchg; } /********************ÏÔʾ²¿·Ö***************************/ printf("----------------------------寻找最短路径----------------------------\n"); printf("直达关系如下\n"); for(i=0;i<SquSize;i++) { for(j=0;j<SquSize;j++) printf("%d ",sq1[i][j]); printf("\n"); } while(1) { printf("输入查询位置代码(出发代码,目的代码)"); scanf("%d,%d",&set,&arrive); getchar(); Search(datbox,set,arrive); } return 0; }
findpath.rar
(2.5 KB)