연결된 컴퓨터끼리는 하나의 네트워크이고 연결되지않은 컴퓨터 하나씩 네트워크가 추가된다.
즉 연결된 컴퓨터를 세면 된다 => DFS
#include <string>
#include <vector>
using namespace std;
bool visited[201]={0};
void dfs(int n, vector<vector<int>> computers,int cur){ //연결된함수 모두 방문처리 => 재귀함수로
visited[cur]=true;
//재귀함수
for(int i=0;i<n;i++){ //연결된 함수 찾으려고 전체 순회
if(!visited[i]&&computers[cur][i]==1){ //연결되고 방문x일때
dfs(n,computers,i);
}
}
}
int solution(int n, vector<vector<int>> computers) {
int answer = 0;
//연결된 네트워크를 확인해야함. => dfs 로
for(int i=0;i<n;i++){
if(!visited[i]){
dfs(n,computers,i);
answer++;
}
}
return answer;
}
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스 LV 3](DFS)단어 변환 (0) | 2024.01.26 |
---|---|
[프로그래머스 LV 3](DFS) 여행경로 (0) | 2024.01.25 |
[프로그래머스 LV 2] 게임 맵 최단거리 (0) | 2024.01.24 |
[프로그래머스 LV 2] [3차] 압축 (0) | 2024.01.23 |
[프로그래머스 LV 2]전화번호 목록 C++ (1) | 2024.01.22 |