문제
접근
간단한 BFS 문제이다.
코드
- 파이썬 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def solution(n, computers):
visit = [0] * n
answer = 0
for i in range(n):
if visit[i] == 0:
visit[i] = 1
bfs(i, visit, computers)
answer+=1
return answer
def bfs(i, visit, computers):
queue = []
queue.append(i)
while len(queue) != 0:
com = queue.pop(0)
for i in range(len(computers)):
if computers[com][i] == 1 and visit[i] == 0:
queue.append(i)
visit[i] = 2