邻接矩阵dfs
#include<bits/stdc++.h>
using namespace std; int a[11][11];
bool visited[11]; void store_graph(){//邻接矩阵存储图
int i,j;
for(i = 1; i <= 10; i++)
for(j = 1; j <= 10; j++)
cin>>a[i][j];
} void dfs_graph(){
void dfs(int v);
memset(visited,false,sizeof(visited));
for(int i = 1; i <= 10; i++)
if(visited[i] == false)
dfs(i);
} int Adj(int x){
for(int i =1; i <=10; i++){
if(a[x][i] == 1&& visited[i] == false)
return i;
return 0;
}
} void dfs(int v){
int Adj(int x);
cout<<v<<' ';
visited[v]=true;
int adj = Adj(v);
while(adj != 0){
if(visited[adj] == false)
dfs(adj);
adj = Adj(v);
}
} int main(){
cout<<"初始化图:"<<endl;
store_graph(); cout<<"dfs遍历结果:"<<endl;
dfs_graph(); return 0;
}
dfs
邻接矩阵dfs的更多相关文章
- PAT1013. Battle Over Cities(邻接矩阵、邻接表分别dfs)
//采用不同的图存储结构结构邻接矩阵.邻接表分别dfs,我想我是寂寞了吧,应该试试并查集,看见可以用并查集的就用dfs,bfs代替......怕了并查集了 //邻接矩阵dfs #include< ...
- [数据结构]图的DFS和BFS的两种实现方式
深度优先搜索 深度优先搜索,我们以无向图为例. 图的深度优先搜索(Depth First Search),和树的先序遍历比较类似. 它的思想:假设初始状态是图中所有顶点均未被访问,则从某个顶点v出发, ...
- PAT Advanced A1021 Deepest Root (25) [图的遍历,DFS,计算连通分量的个数,BFS,并查集]
题目 A graph which is connected and acyclic can be considered a tree. The height of the tree depends o ...
- PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]
题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...
- PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]
题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a ...
- POJ 1386&&HDU 1116 Play on Words(我以后再也不用cin啦!!!)
Play on Words Some of the secret doors contain a very interesting word puzzle. The team of archaeolo ...
- 【算法总结】图论/dp-动态规划 大总结
写于一只蹲在角落的蒟蒻-Z__X... 2020.2.7,图论和 \(dp\) 终于告一段落.蓦然回首,好似已走过许多...不曾细细品味,太多太多又绵延不断地向我涌来... 谨以此纪念 逝去 的图论和 ...
- 图结构练习——判断给定图是否存在合法拓扑序列(dfs算法(第一个代码),邻接矩阵(前两个代码),邻接表(第三个代码))
sdut 2140 图结构练习——判断给定图是否存在合法拓扑序列 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 给定一个有向图 ...
- 数据结构学习笔记05图 (邻接矩阵 邻接表-->BFS DFS、最短路径)
数据结构之图 图(Graph) 包含 一组顶点:通常用V (Vertex) 表示顶点集合 一组边:通常用E (Edge) 表示边的集合 边是顶点对:(v, w) ∈E ,其中v, w ∈ V 有向边& ...
随机推荐
- 第七天python3 函数、参数及参数解构(二)
函数参数 参数规则: 参数列表参数一般顺序是:普通参数<--缺省参数<--可变位置参数<--keyword-only参数(可带缺省值)<--可变关键字参数 def fn(x,y ...
- 使用Pure Pipes来替换HTML里面的纯函数
<ul *ngFor="let item of list"> <li>{{show(item.label)}}</li> </ul> ...
- Windows 安装 Linux 环境
简介 在实际开发中,我们除了在Windows上进行开发外,可能还需要基于Linux进行一些编译或者测试等,因此,我们可能需要在Windows环境中安装Linux环境,通常可能我们会使用虚拟机替代,但是 ...
- 快速体验Spring Boot了解使用、运行和打包 | SpringBoot 2.7.2学习系列
SpringBoot 2.7.2 学习系列,本节内容快速体验Spring Boot,带大家了解它的基本使用.运行和打包. Spring Boot 基于 Spring 框架,底层离不开 IoC.AoP ...
- [NCTF2019]SQLi-1||SQL注入
1.打开之后首先尝试万能密码登录和部分关键词(or.select.=.or.table.#.-等等)登录,显示被检测到了攻击行为并进行了拦截,结果如下: 2.使用dirmap进行目录扫描,发现robo ...
- 青峰Flutter视频播放软件
下载地址: https://github.com/patton88/peak_flutter_player/raw/master/peak_flutter_player_v1.1.5_release0 ...
- 金秋十月 - Apache DolphinScheduler 收获 2 位新 Committer
点击上方蓝字关注 Apache DolphinScheduler Apache DolphinScheduler(incubating),简称"DS", 中文名 "海豚调 ...
- [极客大挑战 2019]HardSQL-1
1.打开之后万能密码等均被过滤,那就先确定下过滤的内容,采用brup抓包进行爆破,发现对union进行了过滤,因此这里就没法使用联合注入,结果如下: 爆破得字典: ^ & && ...
- Luogu1099 树网的核 (暴力?,floyd?)(还未想正解,暴力就A了)
阅读理解两小时,手敲暴力思考5分钟.然后\(n^3\)就A了 暴力代码 #include <iostream> #include <cstdio> #include <c ...
- MySQL编译安装-出现错误提示
环境: 系统:centos7.6 MySQL:5.6.3 cmake:2.8.6 原因: 安装ncurses-devel运行环境 [root@localhost ~]# yum -y install ...