zoukankan      html  css  js  c++  java
  • 2107=数据结构实验之图论二:图的深度遍历

     1 #include <stdio.h>
     2 #include <string.h>
     3 int map[100][100];
     4 int visit[100];
     5 int k,n;//k在这里定义防止在DFS函数中再次定义使代码繁琐。
     6 void DFS(int t)//DFS函数(深度优先搜索)。
     7 {
     8     int i;//t表示此时我们处于哪个“点”。
     9     visit[t]=1;//将数组中的值变为1,表示已经搜索过。
    10     for(i=0;i<k;i++)//从0开始即先遍历节点编号小的。
    11     {
    12         if(map[t][i]==1&&visit[i]==0)//当此点可以通过并没有搜索过时,输出此点。
    13         {
    14             printf(" %d",i);
    15             DFS(i);//递归。
    16         }
    17     }
    18 }
    19 int main()
    20 {
    21     int T,i;
    22     int a,b;
    23     scanf("%d",&T);
    24     while(T--)
    25     {
    26         memset(map,0,sizeof(map));
    27         memset(visit,0,sizeof(visit));//将两个数组中的数据全部初始化为0。
    28         scanf("%d %d",&k,&n);
    29         for(i=0;i<n;i++)
    30         {
    31             scanf("%d %d",&a,&b);//a,b两个暂时储存数据。
    32             map[a][b]=map[b][a]=1;//因为是无向边,路径为双向,并在二维数组中将两点的值变为1,表示可以通过。
    33         }
    34         printf("0");//0作为起始点,直接输出,在DFS函数中从0开始搜索。
    35         DFS(0);
    36         printf("
    ");
    37     }
    38     return 0;
    39 }
  • 相关阅读:
    108.Convert Sorted Array to Binary Search Tree
    111.Minimum Depth of Binary Tree
    118.Pascal's Triangle
    122.Best Time to Buy and Sell Stock II
    35.搜索插入位置
    OSI参考模型
    虚拟机访问tomcat
    linux输入ifconfig找不到IP的解决办法
    分层协议、协议、接口、服务
    Spring Boot项目的创建
  • 原文地址:https://www.cnblogs.com/Angfe/p/10392529.html
Copyright © 2011-2022 走看看