[Python] 백준 2146번 - 다리 만들기
알고리즘/BFS & DFS·2025. 8. 15.
문제https://www.acmicpc.net/problem/2146 문제풀이BFS 1회차 → 모든 섬에 고유 번호 부여BFS 2회차 → 각 섬에서 출발해 다른 섬까지의 최단 거리 계산각 섬별로 BFS 수행 후 최소 거리 갱신최소 다리 길이 출력 CODEimport sysfrom collections import dequeinput = sys.stdin.readlinen = int(input())graph = []for _ in range(n): graph.append(list(map(int, input().split())))visited = [[False] * n for _ in range(n)]cnt = 1result = 1e9dx = [-1, 1, 0, 0]dy = [0, 0, -1, 1..