Skip to content

Commit 037b53a

Browse files
committed
[level 3] Title: 네트워크, Time: 0.76 ms, Memory: 86.3 MB -BaekjoonHub
1 parent 4bbf292 commit 037b53a

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# [level 3] 네트워크 - 43162
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/43162)
4+
5+
### 성능 요약
6+
7+
메모리: 86.3 MB, 시간: 0.76 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 깊이/너비 우선 탐색(DFS/BFS)
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2025년 09월 18일 14:19:05
20+
21+
### 문제 설명
22+
23+
<p>네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있을 때 컴퓨터 A와 컴퓨터 C도 간접적으로 연결되어 정보를 교환할 수 있습니다. 따라서 컴퓨터 A, B, C는 모두 같은 네트워크 상에 있다고 할 수 있습니다.</p>
24+
25+
<p>컴퓨터의 개수 n, 연결에 대한 정보가 담긴 2차원 배열 computers가 매개변수로 주어질 때, 네트워크의 개수를 return 하도록 solution 함수를 작성하시오.</p>
26+
27+
<h5>제한사항</h5>
28+
29+
<ul>
30+
<li>컴퓨터의 개수 n은 1 이상 200 이하인 자연수입니다.</li>
31+
<li>각 컴퓨터는 0부터 <code>n-1</code>인 정수로 표현합니다.</li>
32+
<li>i번 컴퓨터와 j번 컴퓨터가 연결되어 있으면 computers[i][j]를 1로 표현합니다.</li>
33+
<li>computer[i][i]는 항상 1입니다.</li>
34+
</ul>
35+
36+
<h5>입출력 예</h5>
37+
<table class="table">
38+
<thead><tr>
39+
<th>n</th>
40+
<th>computers</th>
41+
<th>return</th>
42+
</tr>
43+
</thead>
44+
<tbody><tr>
45+
<td>3</td>
46+
<td>[[1, 1, 0], [1, 1, 0], [0, 0, 1]]</td>
47+
<td>2</td>
48+
</tr>
49+
<tr>
50+
<td>3</td>
51+
<td>[[1, 1, 0], [1, 1, 1], [0, 1, 1]]</td>
52+
<td>1</td>
53+
</tr>
54+
</tbody>
55+
</table>
56+
<h5>입출력 예 설명</h5>
57+
58+
<p>예제 #1<br>
59+
아래와 같이 2개의 네트워크가 있습니다.<br>
60+
<img src="https://grepp-programmers.s3.amazonaws.com/files/ybm/5b61d6ca97/cc1e7816-b6d7-4649-98e0-e95ea2007fd7.png" title="" alt="image0.png"></p>
61+
62+
<p>예제 #2<br>
63+
아래와 같이 1개의 네트워크가 있습니다.<br>
64+
<img src="https://grepp-programmers.s3.amazonaws.com/files/ybm/7554746da2/edb61632-59f4-4799-9154-de9ca98c9e55.png" title="" alt="image1.png"></p>
65+
66+
67+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
boolean[] visited;
5+
public int solution(int n, int[][] computers) {
6+
visited = new boolean[n];
7+
int answer = 0;
8+
for(int i = 0; i < n; i ++){
9+
if(!visited[i]){
10+
dfs(i,computers);
11+
answer ++;
12+
}
13+
}
14+
15+
return answer;
16+
}
17+
18+
void dfs(int idx , int[][] computers) {
19+
20+
int[] computer = computers[idx];
21+
22+
for(int i =0; i < computer.length; i ++) {
23+
// 연결된 컴퓨터를 찾음
24+
if(computer[i] == 1 && !visited[i]) {
25+
visited[idx] = true;
26+
dfs(i,computers);
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)