알고리즘

[DFS/BFS] 네트워크

이진지니지니진 2023. 2. 16. 22:59

프로그래머스 Level 3

DFS / BFS 문제들이 어려워서 집중적으로 풀어보고자 프로그래머스 <코딩테스트 고득점 Kit>에서 집중적으로 DFS, BFS 문제들을 풀어보았다 !!

 

<네트워크>

문제 설명

from collections import deque
def bfs(computers, x, y):
    q = deque()
    q.append((x, y))
    while q:
        x, y = q.popleft()
        for i in range(len(computers[x])):
            if computers[i][x] == 1:
                computers[x][y] = 0
                computers[y][x] = 0
                q.append((i, x))
def solution(n, computers):
    answer = 0
    for i in range(n):
        for j in range(len(computers[i])):
            if computers[i][j] == 1:
                bfs(computers, i, j)
                answer += 1
    return answer
좀 더 풀어봐야 문제를 봐도 척척 풀겠지만
아직은, 끝까지 잘 풀었다는 거에 의의를 두기로 했다! 🥹
그래도 문제를 계속 풀다보니까 DFS, BFS도 조금씩 자신이 생긴다 ..?