Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

43-pknujsp #176

Merged
merged 7 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions pknujsp/BFS/42-닀리 λ§Œλ“€κΈ°.py
Original file line number Diff line number Diff line change
@@ -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)
50 changes: 50 additions & 0 deletions pknujsp/BRUTE_FORCE/38-μžλ¬Όμ‡ μ™€ μ—΄μ‡ .py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 7 additions & 1 deletion pknujsp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
| 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) |
18 changes: 18 additions & 0 deletions pknujsp/그리디/40-μ„Όμ„œ.py
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions pknujsp/그리디/43-μ €μšΈ.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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))
Original file line number Diff line number Diff line change
@@ -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