diff --git "a/minjeong/DFSBFS/2025-10-19-[\353\260\261\354\244\200]-#17141-\354\227\260\352\265\254\354\206\2142.py" "b/minjeong/DFSBFS/2025-10-19-[\353\260\261\354\244\200]-#17141-\354\227\260\352\265\254\354\206\2142.py" new file mode 100644 index 0000000..228b70d --- /dev/null +++ "b/minjeong/DFSBFS/2025-10-19-[\353\260\261\354\244\200]-#17141-\354\227\260\352\265\254\354\206\2142.py" @@ -0,0 +1,55 @@ +import sys +from collections import deque +from itertools import combinations + +# 상, 하, 좌, 우 +dx = [-1, 1, 0, 0] +dy = [0, 0, -1, 1] + +input = sys.stdin.readline +answer = float("inf") # 최솟값을 찾기 위해 초깃값을 inf로 설정 + + +def bfs(v): + queue = deque(v) + visited = [[-1 for _ in range(N)] for _ in range(N)] + min_value = 0 # 최소 횟수를 찾기 위한 변수 + for x, y in queue: + visited[x][y] = 0 + while queue: + x, y = queue.popleft() + for i in range(4): + ax = x + dx[i] + ay = y + dy[i] + if 0 <= ax < N and 0 <= ay < N: + if visited[ax][ay] == -1 and board[ax][ay] != 1: + queue.append([ax, ay]) + visited[ax][ay] = visited[x][y] + 1 + min_value = max(min_value, visited[x][y] + 1) + + for i in range(N): + for j in range(N): + if visited[i][j] == -1 and board[i][j] != 1: + return float("inf") + return min_value + + +N, M = map(int, input().split()) +board = [list(map(int, input().split())) for _ in range(N)] +virus = [] + +# 바이러스의 좌표 찾기 +for i in range(N): + for j in range(N): + if board[i][j] == 2: + virus.append([i, j]) + +# 바이러스 좌표들 중 M개를 뽑아 BFS 수행 +for v in combinations(virus, M): + answer = min(bfs(v), answer) + +# 정답 반환 +if answer == float("inf"): + print(-1) +else: + print(answer) diff --git "a/minjeong/Heap/2025-10-13-[\353\260\261\354\244\200]-#1916-\354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\260.py" "b/minjeong/Heap/2025-10-13-[\353\260\261\354\244\200]-#1916-\354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\260.py" new file mode 100644 index 0000000..7ac7805 --- /dev/null +++ "b/minjeong/Heap/2025-10-13-[\353\260\261\354\244\200]-#1916-\354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\260.py" @@ -0,0 +1,31 @@ +import sys +import heapq +input = sys.stdin.readline + +# 입력 +N = int(input()) # 도시의 개수 +M = int(input()) # 버스의 개수 +graph = [[] for _ in range(N+1)] +for _ in range(M): + start, end, weight = map(int, input().split()) + graph[start].append([end, weight]) + +# 우리가 구하고자 하는 구간의 출발지와 도착지 번호 +goal_s, goal_e = map(int, input().split()) + +# 최단거리 테이블 초기화 +distances = [float('inf')] * (N+1) +distances[goal_s] = 0 +heap = [] +heapq.heappush(heap, (goal_s, 0)) # 힙(heap)에 (시작노드, 가중치) 추가 + +while heap: + current, dist = heapq.heappop(heap) # 최소힙 + + if distances[current] >= dist: + for node, weight in graph[current]: + if dist + weight < distances[node]: # 지금 발견한 게 더 작으면 + distances[node] = dist + weight + heapq.heappush(heap, (node, dist + weight)) + +print(distances[goal_e]) \ No newline at end of file