定义结点

struct MGraph
{
int vexs[MAXVEX]; //顶点数组
int arc[MAXVEX][MAXVEX]; //邻接矩阵
int numVertex, numEdges; //定点数 边数
};

深度优先遍历

图示

参考代码

bool visited[MAX];
void DFS(MGraph G, int i)
{
cout << G.vexs[i] << " ";
   visited[i] = true;
for (int j = ; j < G.numVertex; ++j)
{
if (G.arc[i][j] == && !visited[j])
DFS(G, j);
}
}
void DFSTranverse(MGraph G)
{
for (int i = ; i < G.numVertex; ++i)
visited[i] = false;
for (int i = ; i < G.numVertex; ++i) //如果是连通图,只执行一次
{
if (!visited[i])
DFS(G, i);
}
}

广度优先遍历

图示

参考代码

void BFSTranverse(MGraph G)
{
queue<int> q;
bool visited[G.numVertex];
for (int i = ; i < G.numVertex; ++i)
visited[i] = false;
for (int i = ; i < G.numVertex; ++i)
{
if (!visited[i])
{
cout << G.vexs[i] << " ";
q.push(i);
visited[i] = true;
while (!q.empty())
{
int k = q.top();
q.pop();
for (int j = ; j < G.numVertex; ++j)
{
if (G.arc[i][j] == && !visitied(j))
{
cout << G.vexs[j] << " ";
visited[j] = true;
q.push(j);
}
}
}
}
   }//for
}

另一种:

void BFS()
{
visited[] = ;
queue q;
q.push();
while(!q.empty())
{
int top = q.front();
cout << top<<" ";//输出
q.pop();
int i ;
for(i = ; i <= M; ++i)
{
if(visited[i] == && matrix[top - ][i - ] == )
{
visited[i] = ;
q.push(i);
}
}
}
}

/******************* 2015.07.06 update ************************/

BFS:

#include <stdio.h>

int o[][] = { { ,  }, { ,  }, { -,  }, { , - } };
int map[][] = { }; int queue[][] = {};
int front = ;
int back = ;
int parent[][][] = {}; void BFS(int sY, int sX, int eY, int eX)
{
queue[back][] = sY;
queue[back++][] = sX;
map[sY][sX] = ;
while (front < back)
{
int Y = queue[front][];
int X = queue[front][];
for (int i = ; i < ; i++)
{
int newY = Y + o[i][];
int newX = X + o[i][];
if (map[newY][newX] == )continue;
if (map[newY][newX] > (map[Y][X] + ))
{
map[newY][newX] = map[Y][X] + ;
parent[newY][newX][] = Y;
parent[newY][newX][] = X;
if ((newY == eY) && (newX == eX))
{
return;
}
queue[back][] = newY;
queue[back++][] = newX;
}
} front++;
}
} int main(int argc, char** argv)
{
freopen("input.txt", "r", stdin);
int N;
scanf("%d\n", &N);
for (int case_num = ; case_num < N; case_num++)
{
for (int i = ; i <= ; i++)
{
for (int j = ; j <= ; j++)
{
scanf("%d\n", &map[i][j]);
if (map[i][j] == )map[i][j] = ;
}
scanf("\n");
}
} BFS(, , , );
if (map[][] == )printf("failed\n");
else printf("%d\n", map[][]); int x = , y = ;
int stack[][] = {};
int step = ;
while (x > || y > )
{
stack[step][] = y;
stack[step++][] = x;
int newY = parent[y][x][];
int newX = parent[y][x][];
x = newX;
y = newY;
} for (int i = step - ; i >= ; i--)
{
printf("%d %d\n", stack[i][], stack[i][]);
}
}

DFS:

#include <stdio.h>

int o[][] = { { ,  }, { ,  }, { -,  }, { , - } };
int map[][] = {};
int minStep = ;
int stack[][] = {};
int step = ;
int minStack[][] = {}; void DFS(int sY, int sX, int eY, int eX)
{
if (step >= minStep)return;
if (map[sY][sX] == )return;
stack[step][] = sY;
stack[step++][] = sX;
map[sY][sX] = ;
if ((sY == eY) && (sX == eX))
{
if (minStep > step)
{
minStep = step;
for (int i = ; i < step; i++)
{
minStack[i][] = stack[i][];
minStack[i][] = stack[i][];
}
}
map[sY][sX] = ;
step--;
return;
}
for (int i = ; i < ; i++)
{
(DFS(sY + o[i][], sX + o[i][], eY, eX));
} map[sY][sX] = ;
step--;
return;
} int main(int argc, char** argv)
{
freopen("input.txt", "r", stdin);
int N;
scanf("%d\n", &N);
for (int case_num = ; case_num < N; case_num++)
{
for (int i = ; i <= ; i++)
{
for (int j = ; j <= ; j++)
{
scanf("%d\n", &map[i][j]);
}
scanf("\n");
}
} DFS(,,,);
printf("%d\n", minStep-);
for (int i = ; i < minStep; i++)
{
printf("%d %d\n", minStack[i][], minStack[i][]);
}
//if (ret)printf("success\n");
//else printf("failed\n");
}

input:


Algorithm --> DFS和BFS的更多相关文章

  1. Clone Graph leetcode java(DFS and BFS 基础)

    题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...

  2. 判断图连通的三种方法——dfs,bfs,并查集

    Description 如果无向图G每对顶点v和w都有从v到w的路径,那么称无向图G是连通的.现在给定一张无向图,判断它是否是连通的. Input 第一行有2个整数n和m(0 < n,m < ...

  3. 数据结构(12) -- 图的邻接矩阵的DFS和BFS

    //////////////////////////////////////////////////////// //图的邻接矩阵的DFS和BFS ////////////////////////// ...

  4. 数据结构(11) -- 邻接表存储图的DFS和BFS

    /////////////////////////////////////////////////////////////// //图的邻接表表示法以及DFS和BFS //////////////// ...

  5. 在DFS和BFS中一般情况可以不用vis[][]数组标记

    开始学dfs 与bfs 时一直喜欢用vis[][]来标记有没有访问过, 现在我觉得没有必要用vis[][]标记了 看代码 用'#'表示墙,'.'表示道路 if(所有情况都满足){ map[i][j]= ...

  6. 图论中DFS与BFS的区别、用法、详解…

    DFS与BFS的区别.用法.详解? 写在最前的三点: 1.所谓图的遍历就是按照某种次序访问图的每一顶点一次仅且一次. 2.实现bfs和dfs都需要解决的一个问题就是如何存储图.一般有两种方法:邻接矩阵 ...

  7. 图论中DFS与BFS的区别、用法、详解?

    DFS与BFS的区别.用法.详解? 写在最前的三点: 1.所谓图的遍历就是按照某种次序访问图的每一顶点一次仅且一次. 2.实现bfs和dfs都需要解决的一个问题就是如何存储图.一般有两种方法:邻接矩阵 ...

  8. 数据结构基础(21) --DFS与BFS

    DFS 从图中某个顶点V0 出发,访问此顶点,然后依次从V0的各个未被访问的邻接点出发深度优先搜索遍历图,直至图中所有和V0有路径相通的顶点都被访问到(使用堆栈). //使用邻接矩阵存储的无向图的深度 ...

  9. dfs和bfs的区别

    详见转载博客:https://www.cnblogs.com/wzl19981116/p/9397203.html 1.dfs(深度优先搜索)是两个搜索中先理解并使用的,其实就是暴力把所有的路径都搜索 ...

随机推荐

  1. PHPmysqli的 预处理执行插入语句

    预编译在mysql端 预编译可以自动防止sql注入攻击 <?php //预编译技术 //1.创建一个mysqli对象 //2.创建myslqi预编译对象 $mysqli=); $mysqli-& ...

  2. Caused by: java.sql.SQLException: Operand should contain 1 column(s)

    1.错误描述 [ERROR:]2015-05-05 15:48:55,847 [异常拦截] org.hibernate.exception.DataException: error executing ...

  3. Caused by: java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration

    1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...

  4. Caused by: java.lang.ClassNotFoundException: javax.persistence.NamedStoredProcedureQuery

    1.错误描述 2014-7-12 21:06:37 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreat ...

  5. LAMP应用部署

    LAMP+wordpress 部署博客 软件安装 yum -y install httpd yum -y install php yum -y install php-mysql yum -y ins ...

  6. Educational Codeforces Round37 E - Connected Components?

    #include <algorithm> #include <cstdio> #include <iostream> #include <queue> ...

  7. 2016弱校联盟十一专场10.3 We don't wanna work!

    能把 not working now 写成 not working hard now 还查一晚上也是没谁了 我的做法是维护两个set 分别是前20% 和后80% #include<iostrea ...

  8. Directory Opus(DO) 个人使用经验 1.0

    设置语言为中文 即时过滤器 设置好之后,在文件目录直接先点击“ ; ”键,然后就可以即时过滤了. 自带的图片查看器查看图片时适应窗口 设置默认窗口 将当前打开的窗口配置为默认窗口,以后每次重新打开DO ...

  9. sass学习笔记--摘录

    //$a: Helvetica, sans-serif //$b: #333 // //body //font: 100% $a //color: $b //$a: red //body //colo ...

  10. 【BZOJ4195】【NOI2015】程序自动分析(并查集)

    [BZOJ4195][NOI2015]程序自动分析(并查集) 题面 Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设 ...