문제 링크 : https://school.programmers.co.kr/learn/courses/30/lessons/43162
메모
- 주어진 모든 컴퓨터들은 computers[i][i]의 값이 1임
-> 이걸 이용해서 방문한 컴퓨터에 대해서는 computers[i][i]의 값을 0으로 바꾸어 방문 표시하기
- dfs로 컴퓨터 방문해서 연결되어 있는 j 컴퓨터들(computers[i][j]의 값이 1인 컴퓨터들)에 대해서 dfs 수행
- dfs 내에 있는 computers[i][i]==0이면 return하는 조건문은 dfs가 중첩되어 수행되는 경우에 필요함
맞힌 코드
#include <bits/stdc++.h>
using namespace std;
void dfs(int i, vector<vector<int>> &computers){
if (computers[i][i]==0)
return;
computers[i][i] = 0;
for (int j=0; j<computers.size(); j++){
if (computers[i][j]==1)
dfs(j, computers);
}
}
int solution(int n, vector<vector<int>> computers) {
int answer = 0;
for (int i=0; i<n; i++){
if (computers[i][i]==1){
dfs(i, computers);
answer++;
}
}
return answer;
}
참고)
[프로그래머스][C++][43162] 네트워크
https://school.programmers.co.kr/learn/courses/30/lessons/43162 1. 문제 설명 네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연
sayho98.tistory.com
'Algorithm' 카테고리의 다른 글
[프로그래머스] 42747번 H-Index C++ (0) | 2024.11.13 |
---|---|
[프로그래머스] 42748번 K번째수 C++ (0) | 2024.11.12 |
[프로그래머스] 1844번 게임 맵 최단거리 C++ (0) | 2024.11.11 |
[백준/BOJ] 20125번 쿠키의 신체 측정 C++ (0) | 2024.11.07 |
[백준/BOJ] 9655번 돌 게임 C++ (0) | 2024.11.07 |