Skip to content

Commit c2f055d

Browse files
learntosurfMingguriguri
authored andcommitted
[BOJ] #27737.버섯 농장 / 실버1 / 60(X)
1 parent 6932e92 commit c2f055d

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import sys, math
2+
input = sys.stdin.readline
3+
from collections import deque
4+
5+
N, M, K = map(int, input().split())
6+
boards = [list(map(int, input().split())) for _ in range(N)]
7+
8+
def bfs(x, y):
9+
queue = deque([(x, y)])
10+
boards[x][y] = 1 # 방문 처리
11+
areas = 1 # 현재 덩어리 크기
12+
13+
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] # 상하좌우 이동
14+
15+
while queue:
16+
x, y = queue.popleft()
17+
for dx, dy in directions:
18+
nx, ny = x + dx, y + dy
19+
if 0 <= nx < N and 0 <= ny < N and boards[nx][ny] == 0:
20+
boards[nx][ny] = 1 # 방문 처리
21+
queue.append((nx, ny))
22+
areas += 1 # 영역 개수 증가
23+
return areas
24+
25+
total_nums = 0
26+
use_seed = False
27+
28+
# 버섯이 자랄 수 있는 칸 찾기
29+
for i in range(N):
30+
for j in range(N):
31+
if boards[i][j] == 0:
32+
areas = bfs(i, j) # BFS로 덩어리 크기 계산
33+
nums = math.ceil(areas / K) # 필요한 포자 개수
34+
total_nums += nums
35+
use_seed = True # 포자 사용 가능 여부 확인
36+
37+
if not use_seed:
38+
print('IMPOSSIBLE')
39+
elif total_nums <= M:
40+
print('POSSIBLE')
41+
print(M - total_nums) # 남은 포자 개수 출력
42+
else:
43+
print('IMPOSSIBLE')

0 commit comments

Comments
 (0)