图论算法|深度优先搜索理论基础|797.所有可能的路径|广度优先搜索BFS理论基础|200. 岛屿数量

深度优先搜索(dfs)理论基础

  1. dfs是可一个方向去搜,不到黄河不回头,直到遇到绝境了,搜不下去了,在换方向(换方向的过程就涉及到了回溯)。
  2. 递归和回溯是相辅相成的
void dfs(参数) {
    
    
	if (终止条件) {
    
    
	    存放结果;
	    return;
	}
	for (选择:本节点所连接的其他节点) {
    
    
	    处理节点;
	    dfs(图,选择的节点); // 递归
	    回溯,撤销处理结果
	}
}

797.所有可能的路径

https://leetcode.cn/problems/all-paths-from-source-to-target/

有向无环图(DAG):

有环无向图是指在图中存在至少一个环(Cycle)的无向图。环是由一系列相互连接的顶点组成的路径,其中第一个顶点和最后一个顶点相同。换句话说,从一个顶点出发,经过若干边,最终又回到了原始的顶点。
我看到题是懵的,不知道数组每个元素代表什么,后来看了答案才明白:数组中的元素相当于数组下标可以去到的地方,比如示例1,代表0可以去1和2,1可以去3,2可以去3
示例2是 :0可以去4,3,1;1可以去3,2,4;2可以去3;3可以去4,
在这里插入图片描述

class Solution {
    
    
private:
    vector<vector<int>> result; // 收集符合条件的路径
    vector<int> path; // 0节点到终点的路径
    // x:目前遍历的节点
    // graph:存当前的图
    void dfs (vector<vector<int>>& graph, int x) {
    
    
        // 要求从节点 0 到节点 n-1 的路径并输出,所以是 graph.size() - 1
        if (x == graph.size() - 1) {
    
     // 找到符合条件的一条路径
            result.push_back(path);
            return;
        }
        for (int i = 0; i < graph[x].size(); i++) {
    
     // 遍历节点n链接的所有节点
            path.push_back(graph[x][i]); // 遍历到的节点加入到路径中来
            dfs(graph, graph[x][i]); // 进入下一层递归
            path.pop_back(); // 回溯,撤销本节点
        }
    }
public:
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
    
    
        path.push_back(0); // 无论什么路径已经是从0节点出发
        dfs(graph, 0); // 开始遍历
        return result;
    }
};

广度优先搜索BFS理论基础

  1. 广搜(bfs)是一圈一圈的搜索过程
  2. 广搜的搜索方式就适合于解决两个点之间的最短路径问题
  3. 我们仅仅需要一个容器,能保存我们要遍历过的元素就可以
    在这里插入图片描述
    广搜代码模板

int dir[4][2] = {
    
    0, 1, 1, 0, -1, 0, 0, -1}; // 表示四个方向
// grid 是地图,也就是一个二维数组
// visited标记访问过的节点,不要重复访问
// x,y 表示开始搜索节点的下标
void bfs(vector<vector<char>>& grid, vector<vector<bool>>& visited, int x, int y) {
    
    
    queue<pair<int, int>> que; // 定义队列
    que.push({
    
    x, y}); // 起始节点加入队列
    visited[x][y] = true; // 只要加入队列,立刻标记为访问过的节点
    while(!que.empty()) {
    
     // 开始遍历队列里的元素
        pair<int ,int> cur = que.front(); que.pop(); // 从队列取元素
        int curx = cur.first;
        int cury = cur.second; // 当前节点坐标
        for (int i = 0; i < 4; i++) {
    
     // 开始想当前节点的四个方向左右上下去遍历
            int nextx = curx + dir[i][0];
            int nexty = cury + dir[i][1]; // 获取周边四个方向的坐标
            if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue;  // 坐标越界了,直接跳过
            if (!visited[nextx][nexty]) {
    
     // 如果节点没被访问过
                que.push({
    
    nextx, nexty});  // 队列添加该节点为下一轮要遍历的节点
                visited[nextx][nexty] = true; // 只要加入队列立刻标记,避免重复访问
            }
        }
    }

}

200. 岛屿数量

#include <vector>
#include <queue>
using namespace std;

class Solution {
    
    
    int res;
    int xy[4][2] = {
    
    {
    
    1, 0}, {
    
    0, 1}, {
    
    -1, 0}, {
    
    0, -1}}; // 方向数组
    void bfs(vector<vector<char>>& grid, vector<vector<bool>>& isTrue, int x, int y) {
    
    
        queue<pair<int, int>> que;
        que.push({
    
    x, y});
        isTrue[x][y] = true;
        while (!que.empty()) {
    
    
            pair<int, int> cur = que.front();
            que.pop();
            int curx = cur.first;
            int cury = cur.second;
            for (int i = 0; i < 4; i++) {
    
    
                int nextx = curx + xy[i][0]; //使用新的变量来 计算下一步的x坐标
                int nexty = cury + xy[i][1]; // 计算下一步的y坐标
                if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue;
                if (grid[nextx][nexty] == '1' && isTrue[nextx][nexty] == false) {
    
    
                    que.push({
    
    nextx, nexty});
                    isTrue[nextx][nexty] = true;
                }
            }
        }
    }
public:
    int numIslands(vector<vector<char>>& grid) {
    
    
        res = 0;
        vector<vector<bool>> isTrue(grid.size(), vector<bool>(grid[0].size(), false));
        for (int i = 0; i < grid.size(); i++) {
    
    
            for (int j = 0; j < grid[0].size(); j++) {
    
    
                if (isTrue[i][j] == false && grid[i][j] == '1') {
    
    
                    bfs(grid, isTrue, i, j);
                    res++;
                }
            }
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_43541510/article/details/131940737