Skip to content

Commit bf58c5a

Browse files
committed
[level 3] Title: 네트워크, Time: 1.01 ms, Memory: 10.2 MB -BaekjoonHub
1 parent d2184b0 commit bf58c5a

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-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?language=python3)
4+
5+
### 성능 요약
6+
7+
메모리: 10.2 MB, 시간: 1.01 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 깊이/너비 우선 탐색(DFS/BFS)
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2025년 03월 07일 15:41:03
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from collections import deque
2+
3+
def solution(n, computers):
4+
5+
def bfs(start):
6+
q = deque([start])
7+
visited[start] = True
8+
9+
while q:
10+
A = q.popleft()
11+
for neighbor in range(n):
12+
if computers[A][neighbor] == 1 and not visited[neighbor]:
13+
visited[neighbor] = True
14+
q.append(neighbor)
15+
16+
visited = [False] * n
17+
ans = 0
18+
for i in range(n):
19+
if not visited[i]:
20+
bfs(i)
21+
ans += 1
22+
return ans

0 commit comments

Comments
 (0)