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

42-pknujsp #173

Merged
merged 6 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
76 changes: 76 additions & 0 deletions pknujsp/BFS/42-๋‹ค๋ฆฌ ๋งŒ๋“ค๊ธฐ.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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()
# ์œก์ง€ ์˜์—ญ์€ ๋‹ค๋ฆฌ ์˜์—ญ ํƒ์ƒ‰ ์‹œ์— ์ œ์™ธํ•จ
costs[r][c] = -1

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)
Copy link
Collaborator

@H0ngJu H0ngJu Apr 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ตœ์†Ÿ๊ฐ’์˜ ์ดˆ๊ธฐ๊ฐ’์„ ๋‹ค๋ฅธ ๋ ˆํผ๋Ÿฐ์Šค์—๋Š” sys ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ maxsize๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•˜๋”๋ผ๊ณ ์š”
์ด๋ฏธ ์•Œ๊ณ  ๊ณ„์‹ค ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ดˆ๊ธฐ๊ฐ’์„ ๋ญ˜๋กœ ๋‘ฌ์•ผํ• ์ง€ ๊ณ ๋ฏผ์ด์‹ค ๋•Œ๋Š” sys.maxsize๋กœ ๋‘๋Š” ๊ฒƒ๋„ ๊ดœ์ฐฎ์€ ๊ฒƒ ๊ฐ™์•„์š”!


# ๋‹ค๋ฆฌ ํ›„๋ณด ์œ„์น˜๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ์ตœ์†Œ ๋‹ค๋ฆฌ ๊ธธ์ด ๊ณ„์‚ฐ
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)
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
7 changes: 6 additions & 1 deletion pknujsp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@
| 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) |
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)
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