当前位置: 首页 > news >正文

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

98. 所有可达路径// DFS

#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;     
}

相关文章:

  • 《几何原本》命题I.8
  • 【QGIS二次开发】地图显示与交互-01
  • 分类任务和回归任务的区别
  • 第八章 函数
  • LeetCode热题100JS(20/100)第四天|​41. 缺失的第一个正数​|​73. 矩阵置零​|​54. 螺旋矩阵​|​48. 旋转图像​
  • Arm64架构的Linux服务器安装jdk8
  • 叁[3],直线的角度问题
  • HTML-05NPM使用踩坑
  • clickhouse-介绍、安装、数据类型、sql
  • 编写一个基于OpenSSL的SSL/TLS服务端(HTTPS)可运行的完整示例
  • 深度解析 | 2025 AI新突破,物理信息神经网络(PINN):Nature级顶刊的「科研加速器」,70份源码论文速取!
  • run方法执行过程分析
  • 【笔记ing】python
  • 欧盟电动汽车销量下滑5.9%?电动车没人买了?
  • 【零基础到精通Java合集】第十集:List集合框架
  • deepseek使用记录20
  • 辛格迪客户案例 | 深圳善康医药科技GMP培训管理(TMS)项目
  • FlashAttention CUDA不匹配问题(CUDA的正确安装)与编译卡住问题解决方法
  • ⭐算法OJ⭐跳跃游戏【贪心算法】(C++实现)Jump Game 系列 I,II
  • 全球首创!微软发布医疗AI助手,终结手写病历时代
  • 产品营销策划/郑州seo培训班
  • 北京梵客装饰/潍坊seo教程
  • 手机网站 尺寸/2021年热门关键词
  • 免费做外贸网站/问卷调查网站
  • 网站开发兼职成都/常州seo博客
  • 网站怎么快速收录/全国新增确诊病例