Notice
Recent Posts
Recent Comments
-
[프로그래머스] 레벨 3 - 네트워크 본문
https://programmers.co.kr/learn/courses/30/lessons/43162
서로 분리된 그래프의 개수를 찾는 문제다.
양방향 간선으로 연결된 그래프들의 정보가 인접행렬로 주어지는데 이를 활용해
분리된 그래프가 몇 개인지 확인하는 문제다.
나는 BFS로 해답을 찾았다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#include <string>
#include <vector>
#include <queue>
using namespace std;
bool check[200];
int answer = 0;
void falsing(int n){
for(int i=0; i<n; i++) check[i] = false;
}
int solution(int n, vector<vector<int>> computers) {
falsing(n);
for(int i=0; i<n; i++){
if(!check[i]){
queue<int> q;
check[i] = true;
q.push(i);
while(!q.empty()){
int front = q.front();
q.pop();
for(int j=0; j<n; j++){
int whether = computers[front][j];
if(whether == 1 && !check[j]){
check[j] = true;
q.push(j);
}
}
}
answer++;
}
}
return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
'1-4. 프로그래머스' 카테고리의 다른 글
[프로그래머스_레벨 3] 여행 경로 (DFS) (0) | 2020.02.10 |
---|---|
[프로그래머스_레벨 3] 2 x n 타일링 (0) | 2020.02.10 |
[프로그래머스] 레벨 2 - 타겟 넘버 (0) | 2020.02.07 |
[프로그래머스] 레벨 2 - 카펫 (0) | 2020.02.07 |
[프로그래머스] 레벨 2 - H-Index (0) | 2020.02.07 |
Comments