深さ優先探索 (dfs) の理論的基礎
- DF は一方向に探索することができ、黄河に到達するまで、絶望的な状況に遭遇して探索を続行できなくなるまで、後戻りすることはできません。方向を変えることになります (方向を変えるプロセスには後戻りが含まれます)。
- 再帰とバックトラッキングは密接に関係します
void dfs(参数) {
if (终止条件) {
存放结果;
return;
}
for (选择:本节点所连接的其他节点) {
处理节点;
dfs(图,选择的节点); // 递归
回溯,撤销处理结果
}
}
797. 考えられるすべてのパス
https://leetcode.cn/problems/ソースからターゲットへのすべてのパス/
有向非巡回グラフ (DAG):
循環無向グラフとは、グラフ内に少なくとも 1 つのサイクル (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 の理論的基礎
- 広域検索 (bfs) はラウンドバイラウンドの検索プロセスです
- Guangsou の探索方法は 2 点間の最短経路問題を解くのに適しています
- 必要なのは、トラバースしたい要素を保存できるコンテナだけです。
ワイド検索コード テンプレート
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;
}
};