ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • BFS(Breadth First Search)
    Computer Engineering/자료구조 2019. 9. 23. 22:01

    - C++

    #include <iostream>
    #include <queue>
    #include <vector>
    
    using namespace std;
    
    int number = 7;
    int c[7];
    vector<int> a[8];
    
    void bfs(int start) {
        queue<int> q;
        q.push(start);
        c[start] = true;
        while (!q.empty()) {
            int x = q.front();
            q.pop();
            printf("%d", x);
            for(int i=0;i<a[x].size();i++){
                int y = a[x][i];
                if(!c[y]){
                    q.push(y);
                    c[y] = true;
                }
            }
        }
    }
    
    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);
    
        bfs(1);
    
        return 0;
    }
    반응형

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

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

    댓글

Designed by Tistory.