반응형
https://school.programmers.co.kr/learn/courses/30/lessons/43162
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <string>
#include <vector>
#include <iostream>
using namespace std;
vector<int> v;
vector<vector<int>> com;
int nn;
// 1 1 0
// 1 1 0
// 0 0 1
void recur(int i) // 인덱스 하나씩 순회하면서
{
for (int j = 0; j < nn; j++) {
if (v[j] && com[i][j]) { // i, j 가 연결되어 있으면
v[j] = 0;
recur(j); // 또 j랑 연결된 애들 죄다 0 만들기
}
}
}
int solution(int n, vector<vector<int>> computers) {
int ans = 0;
nn = n;
com = computers;
for (int i = 0; i < n; i++)
v.push_back(1); // v에 1로 다 초기화해준다.
for (int i = 0; i < n; i++) {
if (v[i]) {
ans++;
recur(i);
}
}
return ans;
}
v[j]가 이미 처리한 네트워크안에 있으면 재귀 안 타게 조건 추가해 줘야 한다 <- 조심
반응형
'<algorithm> > 프로그래머스' 카테고리의 다른 글
프로그래머스 이중우선순위큐 c++ (0) | 2023.08.06 |
---|---|
프로그래머스 야근 지수 c++ (0) | 2023.07.30 |
프로그래머스 타겟넘버 c++ (2) | 2023.07.02 |
프로그래머스 뉴스 클러스터링 c++ (0) | 2023.07.02 |
프로그래머스 정수 삼각형 c++ (2) | 2023.06.28 |