티스토리 뷰
코딩테스트 연습 - 네트워크
네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있
programmers.co.kr
문제
네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있을 때 컴퓨터 A와 컴퓨터 C도 간접적으로 연결되어 정보를 교환할 수 있습니다. 따라서 컴퓨터 A, B, C는 모두 같은 네트워크 상에 있다고 할 수 있습니다.
컴퓨터의 개수 n, 연결에 대한 정보가 담긴 2차원 배열 computers가 매개변수로 주어질 때, 네트워크의 개수를 return 하도록 solution 함수를 작성하시오.
제한사항
- 컴퓨터의 개수 n은 1 이상 200 이하인 자연수입니다.
- 각 컴퓨터는 0부터 n-1인 정수로 표현합니다.
- i번 컴퓨터와 j번 컴퓨터가 연결되어 있으면 computers[i][j]를 1로 표현합니다.
- computers[i][i]는 항상 1입니다.
입출력 예


문제풀이
DFS 또는 BFS 탐색이 끝나면 하나의 네트워크가 형성된 것이기 때문에 DFS/BFS 탐색 횟수가 답이 되는 문제다.
- 가장 첫번째 노드에 대해서 DFS/BFS 실행
- visited 배열에 방문 여부 표시
- DFS/BFS가 끝나면 네트워크가 형성되지 않은 컴퓨터를 찾아 다시 탐색
- 모든 노드를 방문했다면 프로그램 종료
코드
C++ 코드
This file contains 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 <string> | |
#include <vector> | |
#define MAX 200 | |
using namespace std; | |
bool visited[MAX]; | |
void dfs(vector<vector<int>> &computers, int now) { | |
visited[now] = true; | |
for (int i = now, j = 0; j < computers.size(); j++) { | |
if (i == j) continue; | |
if (!visited[j] && computers[i][j]) { | |
dfs(computers, j); | |
} | |
} | |
} | |
int solution(int n, vector<vector<int>> computers) { | |
int answer = 0; | |
for (int i = 0; i < n; i++) { | |
if (!visited[i]) { | |
answer++; | |
dfs(computers, i); | |
} | |
} | |
return answer; | |
} |
Kotlin 코드
This file contains 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
class Solution { | |
fun solution(n: Int, computers: Array<IntArray>): Int { | |
var answer = 0 | |
val visited = mutableListOf<Int>() | |
for (v in 0 until n) { | |
if (!visited.contains(v)) { | |
dfs(v, computers, visited) | |
answer++ | |
} | |
} | |
return answer | |
} | |
fun dfs(v : Int, computers : Array<IntArray>, visited : MutableList<Int>) { | |
visited.add(v) | |
computers[v].forEachIndexed { next, connected -> | |
if (connected == 1 && !visited.contains(next)) | |
dfs(next, computers, visited) | |
} | |
} | |
} |
'Problem Solving > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 문자열 압축 C++/Kotlin (0) | 2021.08.03 |
---|---|
[프로그래머스] 여행경로 C++ (0) | 2021.07.28 |
[프로그래머스] 표 편집 C++ (0) | 2021.07.15 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- dfs
- 그래프
- 문자열
- 큐
- 스택
- 두 포인터
- 알고리즘
- 배열
- C++
- SWEA
- 투 포인터
- 이분 탐색
- 위상 정렬
- algorithm
- Java
- 자바
- SW Expert Academy
- Kotlin
- 구현
- 백준
- 트리
- 재귀
- BOJ
- programmers
- BFS
- 정렬
- 프로그래머스
- 브루트포스
- Two Pointer
- 분할 정복
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함