栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

西工大NOJ数据结构理论——019.基于图的深度优先搜索策略(耿7.10)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

西工大NOJ数据结构理论——019.基于图的深度优先搜索策略(耿7.10)

临近期末考了,所以思路来不及整理了,但大家还是可以仿照我的上几篇文章,“先动笔,再代码”的思路还是不变的。 

#include
#include
#define MAX_VERTEX_NUM 120
#define FALSE 0
#define TRUE 1
#define YES 1
#define NO 0
int visited[MAX_VERTEX_NUM]={FALSE};
int flag=NO;

typedef struct ArcNode{ //弧 
	int adjvex; //该弧所指向的顶点的位置 
	struct ArcNode *nextarc; //指向下一条弧的指针 
}ArcNode;

typedef struct VNode{ //顶点 
	int data; //顶点信息 
	ArcNode *firstarc; //指向第一条依附该顶点的弧的指针 
}VNode;

typedef struct ALGraph{ //图 
	VNode vertices[MAX_VERTEX_NUM]; //图的各个顶点 
	int vexnum,arcnum; //图的当前结点数和弧数 
}ALGraph;

void graph_initialize(ALGraph* graph){ //图的初始化 
	graph->vexnum=0;
	graph->arcnum=0;
	for(int i=0;ivertices[i].data=0;
		graph->vertices[i].firstarc=NULL;
	}
}

void arcnode_initialize(ArcNode* arcnode){ //弧结点的初始化 
	arcnode->adjvex=0;
	arcnode->nextarc=NULL;
}

ArcNode* arcnode_create(){ //弧结点的创建 
	ArcNode *arcnode=(ArcNode*)malloc(sizeof(ArcNode));
	arcnode_initialize(arcnode);
	return arcnode;
}

ALGraph* graph_create(){ //图的创建 
	ALGraph* graph=(ALGraph*)malloc(sizeof(ALGraph));
	graph_initialize(graph);
	int n,m; //n是顶点数,m是边数 
	scanf("%d %d",&n,&m);
	graph->vexnum=n;
	graph->arcnum=m; 
	for(int i=0;ivexnum;i++){//读入顶点信息 
		int temp;
		scanf("%d",&temp);
		graph->vertices[i].data=temp;
		graph->vertices[i].firstarc=NULL;
	}
	for(int j=0;jarcnum;j++){ //读入弧信息 
		int a,b;
		scanf("%d %d",&a,&b);
		//下面找到弧要插入的位置 
		int i=0;
		while(ivexnum){
			if(graph->vertices[i].data==a) break;
			i++; 
		}
		ArcNode *arcnode=arcnode_create();
		arcnode->adjvex=b;
		//下面用头插法插入一个结点 
		arcnode->nextarc=graph->vertices[i].firstarc;
		graph->vertices[i].firstarc=arcnode;
	}
	return graph;
}

int Recursion(ALGraph* graph,int head,int tail){
	int i;
	for(i=0;ivexnum;i++){
		if(graph->vertices[i].data==head){
			visited[i]=TRUE;
			break;
		}
	}
	ArcNode *temp=graph->vertices[i].firstarc;
	while(temp!=NULL){
		if(temp->adjvex!=tail){
			Recursion(graph,temp->adjvex,tail);
		}else{
			flag=YES;break;
		}
		if(flag==YES) break;
	}
}

int main(){
	ALGraph* graph=graph_create();
	int begin,end;
	scanf("%d %d",&begin,&end); //读入待搜索的弧信息 
	Recursion(graph,begin,end);
	if(flag==YES){
		printf("yes");
	}else{
		printf("no");
	}
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/890409.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号