Skip to content

Commit 382cc7f

Browse files
committed
네트워크 풀이
1 parent f464c34 commit 382cc7f

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

programmers/네트워크/README.md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# 문제
2+
3+
### 문제 설명
4+
5+
네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있을 때 컴퓨터 A와 컴퓨터 C도 간접적으로 연결되어 정보를 교환할 수 있습니다. 따라서 컴퓨터 A, B, C는 모두 같은 네트워크 상에 있다고 할 수 있습니다.
6+
7+
컴퓨터의 개수 n, 연결에 대한 정보가 담긴 2차원 배열 computers가 매개변수로 주어질 때, 네트워크의 개수를 return 하도록 solution 함수를 작성하시오.
8+
9+
### 제한사항
10+
11+
- 컴퓨터의 개수 n은 1 이상 200 이하인 자연수입니다.
12+
- 각 컴퓨터는 0부터 n-1인 정수로 표현합니다.
13+
- i번 컴퓨터와 j번 컴퓨터가 연결되어 있으면 computers[i][j]를 1로 표현합니다.
14+
- computer[i][i]는 항상 1입니다.
15+
16+
### 입출력 예
17+
18+
| n | computers | return |
19+
| --- | --------------------------------- | ------ |
20+
| 3 | [[1, 1, 0], [1, 1, 0], [0, 0, 1]] | 2 |
21+
| 3 | [[1, 1, 0], [1, 1, 1], [0, 1, 1]] | 1 |
22+
23+
### 입출력 예 설명
24+
25+
- [설명](https://programmers.co.kr/learn/courses/30/lessons/43162?language=javascript)
26+
27+
## 풀이
28+
29+
```javascript
30+
function solution(n, computers) {
31+
let answer = "";
32+
const visited = new Array(n + 1).fill(false, 0, n + 1);
33+
const stack = [];
34+
35+
function DFS(index) {
36+
stack[index] = true;
37+
38+
for (let i = 0; i < computers.length; i++) {
39+
if (computers[index][i] === 1 && !stack[i]) {
40+
DFS(i);
41+
}
42+
}
43+
}
44+
45+
for (let i = 0; i < computers.length; i++) {
46+
if (!stack[i]) {
47+
DFS(i);
48+
answer++;
49+
}
50+
}
51+
52+
return answer;
53+
}
54+
```

0 commit comments

Comments
 (0)