如何建设个人网站凡科怎么样,vps安装wordpress后怎样登录,杭州好的vi设计公司,上海网站制作公司报价本题要求实现一个函数#xff0c;对给定的用邻接矩阵存储的无向无权图#xff0c;以及一个顶点的编号v#xff0c;打印以v为起点的一个深度优先搜索序列。 当搜索路径不唯一时#xff0c;总是选取编号较小的邻接点。 本题保证输入的数据#xff08;顶点数量、起点的编号等…本题要求实现一个函数对给定的用邻接矩阵存储的无向无权图以及一个顶点的编号v打印以v为起点的一个深度优先搜索序列。 当搜索路径不唯一时总是选取编号较小的邻接点。 本题保证输入的数据顶点数量、起点的编号等均合法。
函数接口定义
void DFS(struct Graph *g, int v)
其中 g 是图结构体指针v 是起点编号。图结构体定义如下
#define MaxVertexNum 20 // 最大顶点数
struct Graph{int v; // 顶点数量int adj[MaxVertexNum][MaxVertexNum]; //邻接矩阵
}
裁判测试程序样例
#include stdio.h
#include stdlib.h
/** 实际的测试程序中,** MaxVertexNum可能不是20** 但一定是合法的、不会引发内存错误 **/
#define MaxVertexNum 20
struct Graph{int v; // 顶点数量int adj[MaxVertexNum][MaxVertexNum]; //邻接矩阵
};
int visited[MaxVertexNum]; //顶点的访问标记
void DFS(struct Graph *g, int v); //本题要求实现的函数原型
struct Graph* CreateGraph(){ // 创建图int v;scanf(%d,v);struct Graph* g;g malloc(sizeof(struct Graph));if(!g) return NULL;g-v v;for(int i0; iv; i){visited[i] 0; //访问标记清零for(int j0; jv; j)scanf(%d,(g-adj[i][j]));}return g;
}
int main(){struct Graph* g;g CreateGraph();int v;scanf(%d, v);DFS(g, v);return 0;
}
/** 你提交的代码将被嵌在这里 **/输入样例 对于图片中的图以及样例测试程序的输入方式8个顶点的邻接矩阵从1号顶点出发
8
0 1 1 0 0 0 0 1
1 0 0 0 1 0 0 0
1 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 1 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0
1
输出样例
注意总是选取编号小的邻接点
1
0
2
4
7实现代码邻接矩阵
void DFS(struct Graph *g, int v)
{int w;printf(%d\n,v);visited[v]1;for(int i0;ig-v;i){if((g-adj[v][i]!0)(!visited[i])){DFS(g,i);}}
}