문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/43162
코딩테스트 연습 - 네트워크
네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있
programmers.co.kr
간단한 DFS 문제였습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
const int MAX = 200 + 20; | |
bool visited[MAX]; | |
void func(int idx, int n, vector<vector<int>> computers) | |
{ | |
for (int i = 0; i < n; i++) | |
{ | |
if (idx == i | |
|| visited[i] | |
|| computers[idx][i] == 0) | |
{ | |
continue; | |
} | |
visited[i] = true; | |
func(i, n, computers); | |
} | |
} | |
int solution(int n, vector<vector<int>> computers) { | |
int answer = 0; | |
for (int i = 0; i < n; i++) | |
{ | |
if (visited[i]) | |
{ | |
continue; | |
} | |
answer++; | |
visited[i] = true; | |
func(i, n, computers); | |
} | |
return answer; | |
} | |
int main(void) | |
{ | |
vector<vector<int>> computers = { | |
{ 1, 1, 0 }, | |
{ 1, 1, 0 }, | |
{ 0, 0, 1 } | |
}; | |
cout << solution(3, computers) << "\n"; | |
return 0; | |
} |

개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers 코딩테스트 고득점 Kit] 여행경로 (0) | 2021.09.11 |
---|---|
[Programmers 코딩테스트 고득점 Kit] 단어 변환 (0) | 2021.09.10 |
[Programmers 코딩테스트 고득점 Kit] 타겟 넘버 (0) | 2021.09.07 |
[Programmers 위클리 챌린지 6주차] 복서 정렬하기 (0) | 2021.09.06 |
[Programmers 위클리 챌린지 5주차] 모음 사전 (0) | 2021.09.04 |