代码随想录算法训练营 | 图论 | DFS

98. 所有可达路径// DFS

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> result;
vector<int> path;

void dfs(const vector<list<int>> &graph, int i, int target) {
  if (i == target) {
    result.push_back(path);
    return;
  }
  for (int nums : graph[i]) {
    path.push_back(nums);
    dfs(graph, nums, target);
    path.pop_back();
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<list<int>> graph(n + 1);
  int temp, next;
  for (int i = 0; i < m; i++) {
    cin >> temp >> next;
    graph[temp].push_back(next);
  }
  path.push_back(1);
  dfs(graph, 1, n);
  if (result.size() == 0)
    cout<<-1<<endl;
  for (int i = 0; i < result.size(); i++) {
    for (int j = 0; j < result[i].size() - 1; j++) {
      cout << result[i][j] << " ";
    }
    cout << result[i][result[i].size() - 1] << endl;
  }
}

99. 岛屿数量//。。感觉这题没必要拘泥于用什么搜索。。

复制代码
#include<bits/stdc++.h>
using namespace std;


void DFSfindsameIsland(vector<vector<bool>>& finded,const vector<vector<int>>& graph,int a,int b){
    if(a<0||b<0||a>graph.size()-1||b>graph[a].size()-1) return;
    else if(graph[a][b]==0) return;
    else if(graph[a][b]==1){
        if(finded[a][b]==false){
            finded[a][b]=true;
            DFSfindsameIsland(finded,graph,a,b+1);
            DFSfindsameIsland(finded,graph,a+1,b);
            DFSfindsameIsland(finded,graph,a,b-1);
            DFSfindsameIsland(finded,graph,a-1,b); 
        }
        else return;
    }
}
int main(){
    int i,j;
    cin>>i>>j;
    vector<vector<int>> graph(i,vector<int> (j));
    for(int a=0;a<i;a++){
        for(int b=0;b<j;b++){
            cin>>graph[a][b];
        }
    }
    vector<vector<bool>> finded(i,vector<bool> (j,false));
    int result=0;
    for(int a=0;a<i;a++){
        for(int b=0;b<j;b++){
            if(graph[a][b]==1&&!finded[a][b]){
                result++;
                DFSfindsameIsland(finded,graph,a,b);
            }  
        }
    }
    cout<<result<<endl;     
}

100. 岛屿的最大面积//偷懒了。随便拿上一题的代码改改就交了

复制代码
#include<bits/stdc++.h>
using namespace std;


int DFSfindsameIsland(vector<vector<bool>>& finded,const vector<vector<int>>& graph,int a,int b){
    if(a<0||b<0||a>graph.size()-1||b>graph[a].size()-1) return 0;
    else if(graph[a][b]==0) return 0;
    else{
        if(finded[a][b]==false){
            int result=1;
            finded[a][b]=true;
            result+=DFSfindsameIsland(finded,graph,a,b+1);
            result+=DFSfindsameIsland(finded,graph,a+1,b);
            result+=DFSfindsameIsland(finded,graph,a,b-1);
            result+=DFSfindsameIsland(finded,graph,a-1,b);
            return result;
        }
        else return 0;
    }
}
int main(){
    int i,j;
    cin>>i>>j;
    vector<vector<int>> graph(i,vector<int> (j));
    for(int a=0;a<i;a++){
        for(int b=0;b<j;b++){
            cin>>graph[a][b];
        }
    }
    vector<vector<bool>> finded(i,vector<bool> (j,false));
    int result=0;int maxaera=0;
    for(int a=0;a<i;a++){
        for(int b=0;b<j;b++){
            if(graph[a][b]==1&&!finded[a][b]){
                maxaera=max(maxaera,DFSfindsameIsland(finded,graph,a,b));
            }  
        }
    }
    cout<<maxaera<<endl;     
}
相关推荐
py有趣4 分钟前
LeetCode算法学习之旋转矩阵
学习·算法·leetcode
三花聚顶<>4 分钟前
310.力扣LeetCode_ 最小高度树_三种方法总结
算法·leetcode·职场和发展
萘柰奈5 分钟前
LeetCode----200.岛屿数量(Medium)
算法·leetcode·职场和发展
MonkeyKing_sunyuhua8 分钟前
量化只支持a8w8和w4a8,其中a8w8和w4a8是什么意思?
人工智能·算法
未来之窗软件服务19 分钟前
幽冥大陆(三十九)php二维数组去重——东方仙盟筑基期
android·开发语言·算法·php·仙盟创梦ide·东方仙盟·东方仙盟sdk
DFT计算杂谈22 分钟前
Abinit-10.4.7安装教程
linux·数据库·python·算法·matlab
sali-tec31 分钟前
C# 基于halcon的视觉工作流-章65 点云匹配-基于形状
开发语言·人工智能·算法·计算机视觉·c#
AI科技星42 分钟前
自然本源——空间元、氢尺、探针与场方程
数据结构·人工智能·算法·机器学习·计算机视觉
小O的算法实验室1 小时前
2025年CMAME SCI2区,基于优先级驱动搜索、具备动态候选解管理策略的粒子群算法,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
吃着火锅x唱着歌1 小时前
LeetCode 2874.有序三元组中的最大值II
数据结构·算法·leetcode