Skip to content

Commit

Permalink
Merge pull request #173 from AlgoLeadMe/42-pknujsp
Browse files Browse the repository at this point in the history
42-pknujsp
  • Loading branch information
MunbinLee authored May 3, 2024
2 parents 55dc5e8 + 01fbdd4 commit 4303799
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
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)

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

0 comments on commit 4303799

Please sign in to comment.