简单DFS

#include <stdio.h>

const int MAX = 101;
const int CONTACT = 1;
const int vist_true = 1;
const int vist_false = 0;

void vInputGraphNode(int iNum,int iGraph[MAX][MAX]);
void vDealData(int iNum,int iGraph[MAX][MAX]);
void vDFS(int iCurrent,int iNum,int iVist[],int iGraph[MAX][MAX]);

int main(int argc, const char * argv[])
{
    int iNum;
    int iGraph[MAX][MAX];
    
    while (scanf("%d",&iNum))
    {
        vInputGraphNode(iNum,iGraph);
        vDealData(iNum,iGraph);
    }
    
    return 0;
}

void vInputGraphNode(int iNum,int iGraph[MAX][MAX])
{
    for (int i=1; i<=iNum; i++)
    {
        for (int j=1; j<=iNum; j++)
        {
            scanf("%d",&iGraph[i][j]);
        }
    }
}

void vDFS(int iCurrent,int iNum,int iVist[],int iGraph[MAX][MAX])
{
    int j;
    
    iVist[iCurrent] = vist_true;
//  printf("iCurrent = %d\n",iCurrent);
    j = iCurrent + 1;
    while (j <= iNum)
    {
        //是与当前点通路的,且未访问过
        if((CONTACT == iGraph[iCurrent][j]) && (!iVist[j]))
        {
//          printf("j = %d\n",j);
            vDFS(j, iNum, iVist, iGraph);
        }
        j ++;
    }
}

void vDealData(int iNum,int iGraph[MAX][MAX])
{
    int iVist[iNum+1];
    
    //初始化每个节点,未访问过
    for(int i=1; i<=iNum; i++)
    {
        iVist[i] = vist_false;
    }
    for (int i=1; i<=iNum; i++)
    {
        if (vist_false == iVist[i])
        {
            vDFS(i,iNum,iVist,iGraph);
        }
    }
}
 

你可能感兴趣的:(数据结构,c,DFS,深度遍历)