diff --git "a/pknujsp/BFS/42-\353\213\244\353\246\254 \353\247\214\353\223\244\352\270\260.py" "b/pknujsp/BFS/42-\353\213\244\353\246\254 \353\247\214\353\223\244\352\270\260.py" new file mode 100644 index 00000000..478f5364 --- /dev/null +++ "b/pknujsp/BFS/42-\353\213\244\353\246\254 \353\247\214\353\223\244\352\270\260.py" @@ -0,0 +1,73 @@ +from collections import * + +map_size = int(input()) +matrix = [list(map(int, input().split())) for _ in range(map_size)] + +directions = [(1, 0), (0, -1), (-1, 0), (0, 1)] +island_id = 2 +costs = [[0] * map_size for _ in range(map_size)] + +# 다리를 놓을 위치를 저장할 큐 +bridge_queue = deque() + +# 전체 맵을 순회하면서 각 섬에 ID 부여 +for row in range(map_size): + for col in range(map_size): + + if matrix[row][col] != 1: + continue + + # 현재 칸을 시작으로 하는 섬에 고유 ID 할당 + matrix[row][col] = island_id + queue = deque([(row, col)]) + + # BFS를 통해 같은 섬인 육지에 ID 할당 + while queue: + r, c = queue.popleft() + for dr, dc in directions: + nr, nc = r + dr, c + dc + + if not 0 <= nr < map_size or not 0 <= nc < map_size: + continue + + # 다른 섬과 연결된 바다 영역에 접근하면 종료 + if 1 < matrix[nr][nc] < island_id: + print(1) + exit() + + # 새로운 육지 발견 시 큐에 추가 + if matrix[nr][nc] == 1: + queue.append((nr, nc)) + # 육지와 바로 맞닿은 바다 영역을 다리 후보로 추가 + elif matrix[nr][nc] == costs[nr][nc] == 0: + bridge_queue.append((nr, nc, 1, island_id)) + + costs[nr][nc] = 1 + matrix[nr][nc] = island_id + + island_id += 1 + +min_cost = int(1e6) + +# 다리 후보 위치를 순회하며 최소 다리 길이 계산 +while bridge_queue: + r, c, cost, curr_island_id = bridge_queue.popleft() + + for dr, dc in directions: + nr, nc = r + dr, c + dc + + if not 0 <= nr < map_size or not 0 <= nc < map_size: + continue + + # 아직 다리가 놓이지 않은 바다 영역이면 + # 다리 길이를 증가시키고 큐에 추가 + if costs[nr][nc] == 0: + bridge_queue.append((nr, nc, cost + 1, curr_island_id)) + + costs[nr][nc] = cost + 1 + matrix[nr][nc] = curr_island_id + # 다른 섬과 연결된 다리 영역에 접근하였다면 최소 비용을 갱신 + elif matrix[nr][nc] > 1 and matrix[nr][nc] != curr_island_id: + min_cost = min(min_cost, cost + costs[nr][nc]) + +print(min_cost) \ No newline at end of file diff --git "a/pknujsp/BRUTE_FORCE/38-\354\236\220\353\254\274\354\207\240\354\231\200 \354\227\264\354\207\240.py" "b/pknujsp/BRUTE_FORCE/38-\354\236\220\353\254\274\354\207\240\354\231\200 \354\227\264\354\207\240.py" new file mode 100644 index 00000000..e1cc927a --- /dev/null +++ "b/pknujsp/BRUTE_FORCE/38-\354\236\220\353\254\274\354\207\240\354\231\200 \354\227\264\354\207\240.py" @@ -0,0 +1,50 @@ +# 자물쇠의 중간 부분이 모두 1인지 확인 +def is_valid(new_lock): + length = len(new_lock) // 3 + + for r in range(length, length * 2): + for c in range(length, length * 2): + if new_lock[r][c] != 1: + return False + + return True + +def solution(key, lock): + n = len(lock) + k = len(key) + new_lock = [[0] * (n * 3) for _ in range(n * 3)] + + for r in range(n): + for c in range(n): + new_lock[r + n][c + n] = lock[r][c] + + for _ in range(4): + rev_key = key[::-1] + key = [] + for c in range(k): + row = [] + for r in range(k): + row.append(rev_key[r][c]) + key.append(row) + + """ + 열쇠를 돌리는 로직은 한 줄로도 구현가능 합니다 + key = [row for row in zip(*reversed(key))] + """ + + for r in range(n * 2): + for c in range(n * 2): + # 자물쇠에 열쇠를 끼운다 + for i in range(k): + for j in range(k): + new_lock[r + i][c + j] += key[i][j] + + # 자물쇠에 열쇠가 딱 들어갔는지 확인 + if is_valid(new_lock): + return True + + # 자물쇠에서 열쇠를 빼서 복구시킨다 + for i in range(k): + for j in range(k): + new_lock[r + i][c + j] -= key[i][j] + return False \ No newline at end of file diff --git a/pknujsp/README.md b/pknujsp/README.md index 571b70d7..489d3d88 100644 --- a/pknujsp/README.md +++ b/pknujsp/README.md @@ -38,4 +38,10 @@ | 34차시 | 2024.02.12 | BFS | [이분 그래프](https://www.acmicpc.net/problem/1707) | [#135](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/135) | | 35차시 | 2024.02.18 | 그리디 | [선물할인](https://www.acmicpc.net/problem/25947) | [#137](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/137) | | 36차시 | 2024.02.21 | 이진탐색 | [휴게소 세우기](https://www.acmicpc.net/problem/1477) | [#143](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/143) | -| 37차시 | 2024.03.04 | 구현 | [n+1 카드게임](https://school.programmers.co.kr/learn/courses/30/lessons/258707) | [#149](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/149) | \ No newline at end of file +| 37차시 | 2024.03.04 | 구현 | [n+1 카드게임](https://school.programmers.co.kr/learn/courses/30/lessons/258707) | [#149](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/149) | +| 38차시 | 2024.03.08 | BRUTE_FORCE | [자물쇠와 열쇠](https://school.programmers.co.kr/learn/courses/30/lessons/60059) | [#154](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/154) | +| 39차시 | 2024.03.19 | 큐 | [디스크 컨트롤러](https://school.programmers.co.kr/learn/courses/30/lessons/42627) | [#163](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/163) | +| 40차시 | 2024.03.22 | 그리디 | [센서](https://www.acmicpc.net/problem/2212) | [#168](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/168) | +| 41차시 | 2024.03.25 | 다익스트라 | [알고스팟](https://www.acmicpc.net/problem/1261) | [#169](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/169) | +| 42차시 | 2024.03.29 | BFS | [다리 만들기](https://www.acmicpc.net/problem/2146) | [#173](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/173) | +| 43차시 | 2024.04.03 | 그리디 | [저울](https://www.acmicpc.net/problem/2437) | [#175](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/175) | \ No newline at end of file diff --git "a/pknujsp/\352\267\270\353\246\254\353\224\224/40-\354\204\274\354\204\234.py" "b/pknujsp/\352\267\270\353\246\254\353\224\224/40-\354\204\274\354\204\234.py" new file mode 100644 index 00000000..07494620 --- /dev/null +++ "b/pknujsp/\352\267\270\353\246\254\353\224\224/40-\354\204\274\354\204\234.py" @@ -0,0 +1,18 @@ +from sys import * + +N = int(stdin.readline()) +K = int(stdin.readline()) +SENSORS = sorted(set(map(int, stdin.readline().split()))) + +if K >= N: + print(0) + exit() + +distances = [SENSORS[i] - SENSORS[i - 1] for i in range(1, len(SENSORS))] +distances.sort(reverse=True) + +result = 0 +for i in range(K - 1, len(distances)): + result += distances[i] + +print(result) diff --git "a/pknujsp/\352\267\270\353\246\254\353\224\224/43-\354\240\200\354\232\270.py" "b/pknujsp/\352\267\270\353\246\254\353\224\224/43-\354\240\200\354\232\270.py" new file mode 100644 index 00000000..8000dc85 --- /dev/null +++ "b/pknujsp/\352\267\270\353\246\254\353\224\224/43-\354\240\200\354\232\270.py" @@ -0,0 +1,13 @@ +N = int(input()) +weights = list(map(int, input().split())) +weights.sort() + +x = 0 +for weight in weights: + # x + 1이 현재 추의 무게보다 작다면, 측정이 불가능함 + if x + 1 < weight: + break + + x += weight + +print(x + 1) \ No newline at end of file diff --git "a/pknujsp/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/41-\354\225\214\352\263\240\354\212\244\355\214\237.py" "b/pknujsp/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/41-\354\225\214\352\263\240\354\212\244\355\214\237.py" new file mode 100644 index 00000000..364af0a3 --- /dev/null +++ "b/pknujsp/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/41-\354\225\214\352\263\240\354\212\244\355\214\237.py" @@ -0,0 +1,33 @@ +from heapq import * +from sys import * + +C, R = map(int, stdin.readline().split()) +arr = [list(map(int, list(stdin.readline().strip()))) for _ in range(R)] + +drc = ((1,0),(-1,0),(0,1),(0,-1)) +visited = [[False] * C for _ in range(R)] +heap = [(0, 0, 0)] +target_r = R - 1 +target_c = C - 1 + +while heap: + cost, r, c = heappop(heap) + + if r == target_r and c == target_c: + print(cost) + break + if visited[r][c]: + continue + + visited[r][c] = True + + for dr, dc in drc: + nr = r + dr + nc = c + dc + + if not 0 <= nr < R or not 0 <= nc < C: + continue + if visited[nr][nc]: + continue + + heappush(heap, (cost + arr[nr][nc], nr, nc)) diff --git "a/pknujsp/\355\201\220/39-\353\224\224\354\212\244\355\201\254 \354\273\250\355\212\270\353\241\244\353\237\254.py" "b/pknujsp/\355\201\220/39-\353\224\224\354\212\244\355\201\254 \354\273\250\355\212\270\353\241\244\353\237\254.py" new file mode 100644 index 00000000..0a0b4d8e --- /dev/null +++ "b/pknujsp/\355\201\220/39-\353\224\224\354\212\244\355\201\254 \354\273\250\355\212\270\353\241\244\353\237\254.py" @@ -0,0 +1,24 @@ +from heapq import * +from collections import * + +def solution(jobs): + jobs = deque(sorted(jobs)) + jobs_num = len(jobs) + + curr_time = wait_time = 0 + heap = [] + + while heap or jobs: + while jobs and jobs[0][0] <= curr_time: + requested_time, duration = jobs.popleft() + heappush(heap, (duration, requested_time)) + + if heap: + duration, requested_time = heappop(heap) + + curr_time += duration + wait_time += curr_time - requested_time + else: + curr_time += 1 + + return wait_time // jobs_num \ No newline at end of file