[파이썬]프로그래머스 level3 네트워크

Byeonghyeon Kim·2021년 6월 25일
0

알고리즘문제

목록 보기
90/93
post-thumbnail

링크

프로그래머스 level3 네트워크


처음엔 문제를 대충보고 아 2차원 bfs문제이구나 했는데 틀려서 문제를 다시 살펴봤다.

다시보니 주어지는 2차원 배열은 인접행렬이었다.
모든 컴퓨터를 순회하며 연결되는 것이 있는지 bfs를 통해 탐색해 풀 수 있었다.


정답 코드

from collections import deque

def solution(n, computers):
    ans = 0
    visit = [0] * (n + 1)
    q = deque()
    for i in range(n):
        if not visit[i]:
            ans += 1
            visit[i] = 1
            q.append(i)
            while q:
                r = q.popleft()
                for c in range(n):
                    if r == c: continue
                    if computers[r][c] == 1 and not visit[c]:
                        visit[c] = 1
                        q.append(c)
    return ans

알게된 것👨‍💻

  • 인접행렬에서의 bfs
profile
자기 주도 개발전 (개발, 발전)

0개의 댓글