邻接矩阵
const int MAXV = 1000;//最大顶点数
const int INF = 100000000;
int n, G[MAXV][MAXV];//G为邻接矩阵
bool vis[MAXV] = { false };//顶点vis[i]已访问的话为true
void DFS(int u, int depth) {//u为当前访问的顶点编号,depth为深度
vis[u] = true;//设置已被访问
//可以在此对u进行额外操作
//下面对所有从u出发能达到的分支顶点进行枚举
for (int v = 0; v < n; v++) {//对所有顶点
if (vis[v] == false && G[u][v] != INF) {//顶点未访问而且u可到达
DFS(v, depth + 1);//访问v,深度+1
}
}
}
void DFSTrave() {//遍历图
for (int u = 0; u < n; u++) {//对每个顶点
if (vis[u] == false) {//如果u未访问
DFS(u, 1);//访问u和u所在的连通块,1表示初始为第一层
}
}
}
邻接表:
const int MAXN = 1000;
const int INF = 10000000;
vector<int> Adj[MAXN];//G的邻接表
int n;//定点数
bool vis[MAXN] = { false };
void DFS(int u, int depth) {
vis[u] = true;
for (int i = 0; i < Adj[u].size(); i++) {
int v = Adj[u][i];
if (vis[v] == false) {
DFS(v, depth + 1);
}
}
}
//便利图
void DFSTrave() {
for (int u = 0; u < n; u++) {
if (vis[u] == false) {
DFS(u, 1);
}
}
}
BFS。搜索01矩阵中1的连通块:
#include "pch.h"
#include<cstdio>
#include<queue>
using namespace std;
const int maxn = 100;
struct node {
int x, y;
}Node;
int n, m;
int matrix[maxn][maxn];
bool inq[maxn][maxn] = { false };
int X[4] = { 0,0,1,-1 };
int Y[4] = { 1,-1,0,0 };
bool judge(int x, int y) {//判断坐标[x,y)是否要访问
if (x >= n || x < 0 || y >= m || y < 0) return false;
if (matrix[x][y] == 0 || inq[x][y] == true) return false;
return true;
}
void bfs(int x, int y)
{
queue<node> Q;
Node.x = x, Node.y = y;
Q.push(Node);
inq[x][y] = true;
while (!Q.empty()) {
node top = Q.front();
Q.pop();
for (int i = 0; i < 4; i++) {
int newX = top.x + X[i];
int newY = top.y + Y[i];//得到下一步的四个可能位置
if (judge(newX, newY)) {//测试这些位置是否可达
Node.x = newX;
Node.y = newY;
Q.push(Node);
inq[newX][newY] = true;//可达就入队,同时记录防止多次入队
}
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
scanf("%d", &matrix[x][y]);
}
}
int ans = 0;
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
if (matrix[x][y] == 1 && inq[x][y] == false) {
ans++;//连通块加一
bfs(x, y);
}
}
}
printf("%d\n", ans);
return 0;
}
BFS求走出迷宫的最小步数
#include "pch.h"
#include<cstdio>
#include<string>
#include<queue>
using namespace std;
const int maxn = 100;
struct node {
int x, y;
int step;//step为从起点S到xy的步数(层数)
}S,T,Node;//S为起点 T为终点 Node为临时节点
int m, n;//行列
char maze[maxn][maxn];//迷宫
bool inq[maxn][maxn] = { false };
int X[4] = { 0,0,1,-1 };
int Y[4] = { 1,-1,0,0 };
bool test(int x, int y) {
if (x >= n || x < 0 || y >= m || y < 0) return false;
if (maze[x][y] == '*') return false;//wall
if (inq[x][y] == true) return false;//已入过队列
return true;
}
int bfs() {
queue<node> q;
q.push(Node);
while (!q.empty()) {
node top = q.front();
q.pop();
if (top.x == T.x && top.y == T.y) {
return top.step;//到终点
}
for (int i = 0; i < 4; i++) {
int newX = top.x + X[i];
int newY = top.y + Y[i];
if (test(newX, newY)) {
Node.x = newX;
Node.y = newY;
Node.step = top.step + 1;
q.push(Node);
inq[newX][newY] = true;
}
}
}
return -1;//无法到达终点
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
getchar();//过滤每行后的换行符
for (int j = 0; j < m; j++) {
maze[i][j] = getchar();
}
maze[i][m + 1] = '\0';
}
scanf("%d%d%d%d", &S.x, &S.y, &T.x, &T.y);
S.step = 0;
printf("%d\n", bfs());
return 0;
}