ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • DFS(Depth First Search)
    Computer Engineering/자료구조 2019. 9. 23. 22:43
    #include <iostream>
    #include <queue>
    #include <vector>
    
    using namespace std;
    
    int number = 7;
    int c[7];
    vector<int> a[8];
    
    void dfs(int x) {
        if(c[x]) return;
        c[x] = true;
        cout << x << ' ';
        for(int i=0;i<a[x].size();i++){
            int y = a[x][i];
            dfs(y);
        }
    }
    
    int main(int argc, const char * argv[]) {
        a[1].push_back(2);
        a[2].push_back(1);
    
        a[2].push_back(3);
        a[3].push_back(2);
    
        a[3].push_back(4);
        a[4].push_back(3);
    
        a[4].push_back(5);
        a[5].push_back(4);
    
        a[5].push_back(6);
        a[6].push_back(5);
    
        a[6].push_back(7);
        a[7].push_back(6);
    
        a[7].push_back(8);
        a[8].push_back(7);
    
        dfs(1);
    
        return 0;
    }

     

    반응형

    'Computer Engineering > 자료구조' 카테고리의 다른 글

    BFS(Breadth First Search)  (0) 2019.09.23
    문자열 뒤집기  (0) 2019.09.07
    재귀적 팩토리얼  (0) 2019.09.07

    댓글

Designed by Tistory.