12주 프로젝트 4 출력 간단한 경로

3908 단어
질문 및 코드:
main.cpp:
/*
 *                        
 *    :mian.cpp
 *      :  
 *    :2015 11 30 
 *     :v1.0
 *
 *    :    G    u v    s       。

 *
 *    : 
 *    : 
 */
#include <stdio.h>
#include <malloc.h>
#include "graph.h"
int visited[MAXV];     //                
void SomePaths(ALGraph *G,int u,int v,int s, int path[],int d)
//d              ,      -1
{
    int w,i;
    ArcNode *p;
    visited[u]=1;
    d++;            //     1
    path[d]=u;              //           
    if (u==v && d==s)           //      
    {
        printf("  ");
        for (i=0; i<=d; i++)
            printf("%d ",path[i]);
        printf("
"); } p=G->adjlist[u].firstarc; //p u while(p!=NULL) { w=p->adjvex; //w u if (visited[w]==0) // , SomePaths(G,w,v,s,path,d); p=p->nextarc; // u } visited[u]=0; // } void DispSomePaths(ALGraph *G,int u,int v, int s) { int i; int path[MAXV]; for (i=0; i<G->n; i++) visited[i]=0; // printf(" %d %d %d :
",u,v,s); SomePaths(G,u,v,s,path,-1); printf("
"); } int main() { ALGraph *G; int A[5][5]= { {0,1,0,1,0}, {1,0,1,0,0}, {0,1,0,1,1}, {1,0,1,0,1}, {0,0,1,1,0} }; // ArrayToList(A[0], 5, G); DispSomePaths(G, 1, 4, 3); return 0; }
#ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED

#define MAXV 100                //      
#define INF 32767       //INF  ∞
typedef int InfoType;

//          
typedef struct
{
    int no;                     //    
    InfoType info;              //      ,         
} VertexType;                   //    

typedef struct                  //    
{
    int edges[MAXV][MAXV];      //    
    int n,e;                    //   ,  
    VertexType vexs[MAXV];      //      
} MGraph;                       //        

//         
typedef struct ANode            //        
{
    int adjvex;                 //       
    struct ANode *nextarc;      //         
    InfoType info;              //       ,        
} ArcNode;

typedef int Vertex;

typedef struct Vnode            //         
{
    Vertex data;                //    
    int count;                  //      ,        
    ArcNode *firstarc;          //      
} VNode;

typedef VNode AdjList[MAXV];    //AdjList      

typedef struct
{
    AdjList adjlist;            //   
    int n,e;                    //     n   e
} ALGraph;                      //       

void ArrayToMat(int *Arr, int n, MGraph &g); //             
void ArrayToList(int *Arr, int n, ALGraph *&); //            
void MatToList(MGraph g,ALGraph *&G);//     g      G
void ListToMat(ALGraph *G,MGraph &g);//    G       g
void DispMat(MGraph g);//      g
void DispAdj(ALGraph *G);//     G

#endif // GRAPH_H_INCLUDED
#include <stdio.h>
#include <malloc.h>
#include "graph.h"
void ArrayToList(int *Arr, int n, ALGraph *&G)
{
    int i,j,count=0;  //count      ,     0    
    ArcNode *p;
    G=(ALGraph *)malloc(sizeof(ALGraph));
    G->n=n;
    for (i=0; i<n; i++)                 //                 
        G->adjlist[i].firstarc=NULL;
    for (i=0; i<n; i++)                 //           
        for (j=n-1; j>=0; j--)
            if (Arr[i*n+j]!=0)      //     , Arr  n×n     ,Arr[i*n+j]  Arr[i][j]
            {
                p=(ArcNode *)malloc(sizeof(ArcNode));   //      *p
                p->adjvex=j;
                p->info=Arr[i*n+j];
                p->nextarc=G->adjlist[i].firstarc;      //       *p
                G->adjlist[i].firstarc=p;
            }

    G->e=count;
}

실행 결과:

좋은 웹페이지 즐겨찾기