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

打印 上一主题 下一主题

主题 1040|帖子 1040|积分 3120

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
98. 所有可达路径// DFS
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. vector<vector<int>> result;
  4. vector<int> path;
  5. void dfs(const vector<list<int>> &graph, int i, int target) {
  6.   if (i == target) {
  7.     result.push_back(path);
  8.     return;
  9.   }
  10.   for (int nums : graph[i]) {
  11.     path.push_back(nums);
  12.     dfs(graph, nums, target);
  13.     path.pop_back();
  14.   }
  15. }
  16. int main() {
  17.   int n, m;
  18.   cin >> n >> m;
  19.   vector<list<int>> graph(n + 1);
  20.   int temp, next;
  21.   for (int i = 0; i < m; i++) {
  22.     cin >> temp >> next;
  23.     graph[temp].push_back(next);
  24.   }
  25.   path.push_back(1);
  26.   dfs(graph, 1, n);
  27.   if (result.size() == 0)
  28.     cout<<-1<<endl;
  29.   for (int i = 0; i < result.size(); i++) {
  30.     for (int j = 0; j < result[i].size() - 1; j++) {
  31.       cout << result[i][j] << " ";
  32.     }
  33.     cout << result[i][result[i].size() - 1] << endl;
  34.   }
  35. }
复制代码
99. 岛屿数量//。。感觉这题没须要拘泥于用什么搜刮。。
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. void DFSfindsameIsland(vector<vector<bool>>& finded,const vector<vector<int>>& graph,int a,int b){
  4.     if(a<0||b<0||a>graph.size()-1||b>graph[a].size()-1) return;
  5.     else if(graph[a][b]==0) return;
  6.     else if(graph[a][b]==1){
  7.         if(finded[a][b]==false){
  8.             finded[a][b]=true;
  9.             DFSfindsameIsland(finded,graph,a,b+1);
  10.             DFSfindsameIsland(finded,graph,a+1,b);
  11.             DFSfindsameIsland(finded,graph,a,b-1);
  12.             DFSfindsameIsland(finded,graph,a-1,b);
  13.         }
  14.         else return;
  15.     }
  16. }
  17. int main(){
  18.     int i,j;
  19.     cin>>i>>j;
  20.     vector<vector<int>> graph(i,vector<int> (j));
  21.     for(int a=0;a<i;a++){
  22.         for(int b=0;b<j;b++){
  23.             cin>>graph[a][b];
  24.         }
  25.     }
  26.     vector<vector<bool>> finded(i,vector<bool> (j,false));
  27.     int result=0;
  28.     for(int a=0;a<i;a++){
  29.         for(int b=0;b<j;b++){
  30.             if(graph[a][b]==1&&!finded[a][b]){
  31.                 result++;
  32.                 DFSfindsameIsland(finded,graph,a,b);
  33.             }  
  34.         }
  35.     }
  36.     cout<<result<<endl;     
  37. }
复制代码
100. 岛屿的最大面积//偷懒了。随便拿上一题的代码改改就交了
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int DFSfindsameIsland(vector<vector<bool>>& finded,const vector<vector<int>>& graph,int a,int b){
  4.     if(a<0||b<0||a>graph.size()-1||b>graph[a].size()-1) return 0;
  5.     else if(graph[a][b]==0) return 0;
  6.     else{
  7.         if(finded[a][b]==false){
  8.             int result=1;
  9.             finded[a][b]=true;
  10.             result+=DFSfindsameIsland(finded,graph,a,b+1);
  11.             result+=DFSfindsameIsland(finded,graph,a+1,b);
  12.             result+=DFSfindsameIsland(finded,graph,a,b-1);
  13.             result+=DFSfindsameIsland(finded,graph,a-1,b);
  14.             return result;
  15.         }
  16.         else return 0;
  17.     }
  18. }
  19. int main(){
  20.     int i,j;
  21.     cin>>i>>j;
  22.     vector<vector<int>> graph(i,vector<int> (j));
  23.     for(int a=0;a<i;a++){
  24.         for(int b=0;b<j;b++){
  25.             cin>>graph[a][b];
  26.         }
  27.     }
  28.     vector<vector<bool>> finded(i,vector<bool> (j,false));
  29.     int result=0;int maxaera=0;
  30.     for(int a=0;a<i;a++){
  31.         for(int b=0;b<j;b++){
  32.             if(graph[a][b]==1&&!finded[a][b]){
  33.                 maxaera=max(maxaera,DFSfindsameIsland(finded,graph,a,b));
  34.             }  
  35.         }
  36.     }
  37.     cout<<maxaera<<endl;     
  38. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

络腮胡菲菲

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表