Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""
BOJ #7569. ํ† ๋งˆํ†  (3์ฐจ์›) (๊ณจ๋“œ5)
https://www.acmicpc.net/problem/7569
์œ ํ˜•: Graph, BFS
"""
# PR ์˜ฌ๋ฆด ๋•Œ ๊ณต๊ฐœ ์˜ˆ์ •
18 changes: 18 additions & 0 deletions _WeeklyChallenges/W23-[BFS]/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## ๐Ÿš€5์›” 3์ฃผ์ฐจ (5/19) ์Šคํ„ฐ๋”” ๋ฐœ์ œ ์ฃผ์ œ: Graph (BFS)
> ๋ฐœ์ œ์ž: ๊น€๋ฏผ์ • (@Mingguriguri)

> [!NOTE]
> ์ฃผ์ œ: Graph (BFS)

### ๐Ÿ—‚๏ธ ์Šคํ„ฐ๋”” ์ž๋ฃŒ
- PDF: [๋ฐ”๋กœ๊ฐ€๊ธฐ](Study_BOJ_7576.pdf)
<img width="500" alt="์Šคํ„ฐ๋””๋ฌธ์ œ" src="https://github.com/user-attachments/assets/5ef6096c-b265-4595-8a0e-033905fc2ea6" />
<img width="500" alt="๋ฐœ์ œ๋ฌธ์ œ" src="https://github.com/user-attachments/assets/c4495d0b-0c96-4bd3-bcd5-a40be6c99f4e" />

### ๐Ÿ“– ๋ฌธ์ œ
- [๋ฐฑ์ค€ #7576. ํ† ๋งˆํ† ](https://www.acmicpc.net/problem/7576): Graph (BFS) / ๊ณจ๋“œ5
- ์ •๋‹ต ์ฝ”๋“œ: [Study_BOJ_7576_ํ† ๋งˆํ† .py](Study_BOJ_7576_ํ† ๋งˆํ† .py)

### ๐Ÿ’ป ๊ณผ์ œ
- [๋ฐฑ์ค€ #7569. ํ† ๋งˆํ† ](https://www.acmicpc.net/problem/7569): Graph (BFS) / ๊ณจ๋“œ5
- ์ •๋‹ต ์ฝ”๋“œ: [Assignment_BOJ_7569_ํ† ๋งˆํ† .py](Assignment_BOJ_7569_ํ† ๋งˆํ† .py)
Binary file added _WeeklyChallenges/W23-[BFS]/Study_BOJ_7576.pdf
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
BOJ #7576. ํ† ๋งˆํ†  (2์ฐจ์›) (๊ณจ๋“œ5)
https://www.acmicpc.net/problem/7576
์œ ํ˜•: Graph, BFS
"""
import sys
from collections import deque
input = sys.stdin.readline

# 1. ์ž…๋ ฅ ์ฒ˜๋ฆฌ
M, N = map(int, input().split()) # ๊ฐ€๋กœ ์นธ ์ˆ˜, ์„ธ๋กœ ์นธ ์ˆ˜
box = [list(map(int, input().split())) for _ in range(N)] # ํ† ๋งˆํ† 

# 2. ์ดˆ๊ธฐ ์„ค์ •
queue = deque([]) # ํ
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)] # ๋ฐฉํ–ฅ๋ฒกํ„ฐ
day = 0 # ์ •๋‹ต์ด ๋‹ด๊ธธ ๋ณ€์ˆ˜

# 3. ํ์— ์ดˆ๊ธฐ ์ต์€ ํ† ๋งˆํ†  ์œ„์น˜ ์ €์žฅ
for i in range(N):
for j in range(M):
if box[i][j] == 1:
queue.append((i, j))

# 4. BFS ํƒ์ƒ‰
while queue:
# ์ฒ˜์Œ ํ† ๋งˆํ†  ๊บผ๋‚ด๊ธฐ
x, y = queue.popleft()

# ์ฒ˜์Œ ํ† ๋งˆํ† ์˜ ์ธ์ ‘ํ•œ ํ† ๋งˆํ†  ์ฐพ๊ธฐ
for dx, dy in directions:
nx, ny = x + dx, y + dy

# ๋ฒ”์œ„ ๋‚ด์— ์žˆ๊ณ , ํ† ๋งˆํ† ๊ฐ€ ์ต์ง€ ์•Š์€ ๊ฒฝ์šฐ
if (0 <= nx < N and 0 <= ny < M) and (box[nx][ny] == 0):
# ์ตํžˆ๊ณ  1 ๋”ํ•ด์ฃผ๋ฉฐ ํšŸ์ˆ˜ ์„ธ๊ธฐ
# ์—ฌ๊ธฐ์„œ ๋‚˜์˜จ ์ œ์ผ ํฐ ๊ฐ’์ด ์ •๋‹ต์ด ๋œ๋‹ค.
box[nx][ny] += box[x][y] + 1 # ์ผ์ˆ˜ ๋ˆ„์ 
queue.append((nx, ny))


# 5. ์ •๋‹ต ๊ตฌํ•˜๊ธฐ
for row in box:
for tomato in row:
# ๋ชจ๋‘ ํƒ์ƒ‰ํ–ˆ์ง€๋งŒ ํ† ๋งˆํ† ๊ฐ€ ๋ชจ๋‘ ์ต์ง€ ์•Š์•˜๋‹ค๋ฉด -1 ์ถœ๋ ฅ
if tomato == 0:
print(-1)
exit()

# ๋‹ค ์ตํ˜”๋‹ค๋ฉด ์ตœ๋Œ“๊ฐ’์ด ์ •๋‹ต
day = max(day, max(row))

# 6. ์ •๋‹ต ์ถœ๋ ฅ
print(day - 1) # ์ฒ˜์Œ์— 1๋กœ ์ต์€ ํ† ๋งˆํ† ๋ฅผ ํ‘œํ˜„ํ–ˆ์œผ๋‹ˆ 1์„ ๋นผ์ค€๋‹ค.