[c++/알고리즘] 프로그래머스 네트워크:BFS

corncheese·2021년 8월 10일
0

알고리즘문제풀이

목록 보기
26/31

#include <string>
#include <vector>
#include <queue>

using namespace std;

int ch[201];
int solution(int n, vector<vector<int>> computers) {
    int answer = 0, x;
    queue<int> Q;

    for(int i=0; i<computers.size(); i++){

        if(ch[i] == 0){
            Q.push(i);
            ch[i] = 1;
            answer++;
        }

        while(!Q.empty()){
            x = Q.front();
            Q.pop();

            for(int j=0; j<computers[x].size(); j++){
                if(computers[x][j] == 1 && ch[j] == 0){
                    Q.push(j);
                    ch[j] = 1;
                }
            }
        }
    }

    return answer;
}

0개의 댓글