| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 813 人关注过本帖
标题:练习写的代码,帮我瞧瞧吧~
只看楼主 加入收藏
笨拉登
Rank: 1
等 级:新手上路
帖 子:23
专家分:3
注 册:2008-11-4
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:3 
练习写的代码,帮我瞧瞧吧~
几个月前学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)//&frac12;&laquo;r[i][j]&Ouml;±&frac12;&Oacute;&Ouml;&Atilde;&Aacute;&atilde;
        *(*(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;
    }
    /********************&Iuml;&Ocirc;&Ecirc;&frac34;&sup2;&iquest;·&Ouml;***************************/
    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)
搜索更多相关主题的帖子: 最短路径 可达性矩阵 
2009-08-16 02:09
airhiphop
Rank: 4
来 自:江苏南通
等 级:业余侠客
帖 子:69
专家分:256
注 册:2007-11-16
收藏
得分:10 
我也刚开始研究数据结构,对于算法的好坏我不能评价

但是有几个小建议我要提一下
你应该在你的开头处写上你这个程序的整体思路,大致的过程
你没有对malloc的返回值进行检测

2009-08-16 05:38
笨拉登
Rank: 1
等 级:新手上路
帖 子:23
专家分:3
注 册:2008-11-4
收藏
得分:0 
谢谢你的提醒,忘记检查返回值了。大体思路嘛,是利用复合关系运算出每两个点间是否连通,长度为多少
2009-08-16 11:58
幻影天擎
Rank: 2
等 级:论坛游民
帖 子:13
专家分:13
注 册:2009-8-16
收藏
得分:10 
个位大大们``
我还刚刚开始学习`这个``
想问下`
` 最基本的得先学些什么比较适合我呢``~?????????
2009-08-16 20:32
快速回复:练习写的代码,帮我瞧瞧吧~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020059 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved