代码随想录算法训练营 | 图论 | 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;     
}
相关推荐
guozhetao3 分钟前
【ST表、倍增】P7167 [eJOI 2020] Fountain (Day1)
java·c++·python·算法·leetcode·深度优先·图论
吃着火锅x唱着歌5 分钟前
LeetCode 611.有效三角形的个数
算法·leetcode·职场和发展
CHANG_THE_WORLD3 小时前
金字塔降低采样
算法·金字塔采样
不知天地为何吴女士5 小时前
Day32| 509. 斐波那契数、70. 爬楼梯、746. 使用最小花费爬楼梯
算法
小坏坏的大世界5 小时前
C++ STL常用容器总结(vector, deque, list, map, set)
c++·算法
励志要当大牛的小白菜7 小时前
ART配对软件使用
开发语言·c++·qt·算法
qq_513970448 小时前
力扣 hot100 Day56
算法·leetcode
PAK向日葵8 小时前
【算法导论】如何攻克一道Hard难度的LeetCode题?以「寻找两个正序数组的中位数」为例
c++·算法·面试
爱喝矿泉水的猛男11 小时前
非定长滑动窗口(持续更新)
算法·leetcode·职场和发展
YuTaoShao11 小时前
【LeetCode 热题 100】131. 分割回文串——回溯
java·算法·leetcode·深度优先