首页 > 编程语言 >交个崔鹏题 OJ实践1-C /图的广度搜索/C++

交个崔鹏题 OJ实践1-C /图的广度搜索/C++

时间:2023-12-21 18:55:07浏览次数:22  
标签:崔鹏题 OJ int Graph nd C++ next vertex struct

#include<iostream> 
#include<malloc.h>
#include<queue>
using namespace std;
#define MAX 10
typedef int E;
typedef struct Node{
	int nextVex;
	struct Node *next;
}*node;

struct HeadNode{
	E element;
	struct Node *next;
};
typedef struct GraphTable{
	int vex,edge;
	struct HeadNode vertex[MAX];
} *Graph;
Graph Create(){
	Graph g = (Graph)malloc(sizeof(struct GraphTable));
	g->edge=g->vex=0;
	return g;
}
void addVex(Graph g,E e){
	g->vertex[g->vex].element=e;
	g->vertex[g->vex].next=NULL;
	g->vex++;
}
int getIndexByElem(Graph g,E v){
	int i=0;
	while(true){
		if(g->vertex[i].element==v)
			return i;
		i++;
	}
}
void addEdge(Graph g, E v1, E v2){
	int a=getIndexByElem(g,v1);
	int b=getIndexByElem(g,v2);

	node nd=g->vertex[a].next;
	node newNode=(node)malloc(sizeof(struct Node));
	newNode->next=NULL;
	newNode->nextVex=b;
	if(!nd)
		g->vertex[a].next=newNode;
	else
	{
		while(nd->next)
		{
			if(nd->nextVex==b) return;
			if(nd->next) nd=nd->next;
		}
		nd->next=newNode;
	}
	g->edge++;
}
void printTable(Graph g){
	for(int i=0;i<g->vex;i++){
		cout<<i<<"|"<<g->vertex[i].element;
		node nd=g->vertex[i].next;
		while(nd){
			cout<<"->"<<nd->nextVex;
			nd=nd->next;
		}
		cout<<endl;
	}
	
}
queue<int> Q;

int DFS(Graph g,int startVex,int targetVex,int *visited){
	
	Q.push(g->vertex[startVex].element);
	if(startVex==targetVex) return 1;
	visited[startVex]=1;
	node nd=g->vertex[startVex].next;
	while(nd){
		if(!visited[nd->nextVex])
			if(DFS(g,nd->nextVex,targetVex,visited))
				return 1;
		nd=nd->next;
	}
	return 0;
}
void printQueue(int n){
	for (int i =0;i<n;i++){
		cout<<Q.front();
		if(i<n-1)cout<<" ";
		Q.pop();
	}
	cout<<endl;
}
int main(){
	Graph graph = Create();
	int n;
	cin>>n;
	for(int c=1;c<=n;++c)
		addVex(graph,c);
	int a,b;
	while(cin>>a>>b)
		addEdge(graph,a,b);
	int arr[graph->vex];
	for(int i=0;i<=graph->vex;i++)arr[i]=0;
	DFS(graph,0,n,arr);
	printQueue(n);
	return 0;
}

标签:崔鹏题,OJ,int,Graph,nd,C++,next,vertex,struct
From: https://www.cnblogs.com/Happy-Eric-1/p/17919870.html

相关文章

  • 交个崔鹏题 OJ实践1-B
    #include<iostream>#include<malloc.h>#include<string.h>usingnamespacestd;#defineMAX10#defineINF0;typedefintE;typedefstructGraphMartix{ intvex,edge; intmarixWeight[MAX][MAX]; Edata[MAX];}*Graph;GraphCreate(intv)......
  • 交个崔鹏题 OJ实践1-A
    #include<iostream>#include<malloc.h>usingnamespacestd;typedefintE;typedefstructNode{ Eelement; structNode*next;}*node;voidinitList(nodend){nd->next=NULL;}voiddeleteDup(nodend){if(nd==NULL)return;......
  • C++标准库std::string的find_first_not_of 方法介绍:
    C++标准库std::string 的  find_first_not_of方法介绍: 例如:stra.find_first_not_of(s_fmt_a)在字符串stra中找到第一个不在s_fmt_a字符串中出现过的字符。stra="abc",abc字符都在s_fmt_a字符串里面出现过,所以第一个不在s_fmt_a里的字符是找不到的,返回......
  • HydroOJ 从入门到入土(9)源码简易修改记录——卍解!
    随着OJ的使用越来越深入,本强迫症总会觉得一些细节有时候不那么符合自己的习惯,但是想改又无处下手,最终还是走上了修改源码的邪路.目录0.重要1.超级管理员查看自测代码2.超级管理员隐身查看比赛/作业题目3.超级管理员隐身查看比赛题目列表4.关掉客观题的多选题部......
  • C++ Qt开发:StringListModel字符串列表映射组件
    Qt是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍QStringListModel字符串映射组件的常用方法及灵活运用。QStringListModel是Qt中用于处理字符......
  • [转]CryptoJS-中文文档
    原文地址:CryptoJS-中文文档-掘金原始文档:code.google.com/archive/p/c…介绍CryptoJS是一个JavaScript的加解密的工具包。它支持多种算法:MD5、SHA1、SHA2、SHA3、RIPEMD-160的哈希散列,以及进行AES、DES、Rabbit、RC4、TripleDES加解密。散列算法MD5MD5是一种广泛使......
  • 交个崔鹏题 6-A /c++
    #include<iostream>#defineMAX10#include<malloc.h>#include<string.h>#include<stdlib.h>usingnamespacestd;typedefcharE;typedefstructEdge{ Estart; Eend; intweight;}*Edges;typedefstructGraphMatrix{ intvex,edge......
  • ERROR: Could not build wheels for opencv-python, which is required to install py
    目录系统环境问题描述问题解决问题二参考文章系统环境#macOS系统版本$sw_versProductName:MacOSXProductVersion:10.14.4BuildVersion:18E2035#Python版本$python--versionPython3.9.13问题描述安装opencv-python报错,安装失败#安装opencv-python的命令......
  • C/C++语法基础目录
    目录C/C++语法基础目录前言程序语言简介三大控制结构批量数据存储模块化编程指针自定义数据类型文件面向对象封装继承多态C/C++语法基础目录前言使用过较多的教学书籍,有很多不错的书籍,但是并不是那么切合自身的教学习惯,于是此书被拉开了帷幕。本书的第一次落键是在2023年12月2......
  • 人们一般提到的安全性只涉及内存安全——但这还远远不够……而且与其他语言(包括 C++
    C++之父BjarneStroustrup:我会为全球数十亿行C++代码带来一个崭新的解决方案https://mp.weixin.qq.com/s/L8xYyR88KdHsHqyz_sQ5Sg作者|DavidCassel译者|王强策划|Tina在CppConC++会议上,这位C++的创建人明确了该编程语言中迫切需要的安全措施具体都有哪些。  ......