Skip to content

Commit 535b73c

Browse files
committed
[BOJ]#27737. 버섯농장/실버1/80h
https://www.acmicpc.net/problem/27737
1 parent 27f5b41 commit 535b73c

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Hongjoo/백준/버섯농장.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import math
2+
import sys
3+
from collections import deque
4+
5+
def bfs(y_1 , x_1 , visited) :
6+
# 초기 위치 방문
7+
queue = deque()
8+
queue.append((y_1,x_1))
9+
visited[y_1][x_1] = True
10+
count = 0 # 방문할 node 개수
11+
# 큐 시작
12+
while queue :
13+
y,x =queue.popleft()
14+
count+=1
15+
for k in range(4):
16+
next_y, next_x = y + dy[k] , x + dx[k]
17+
# 필드 범위 포함 확인 & 방문 확인 &이동 가능 지역(0)
18+
if 0<=next_y<N and 0<=next_x<N :
19+
if not visited[next_y][next_x] and not field[next_y][next_x] :
20+
queue.append((next_y, next_x))
21+
visited[next_y][next_x] = True
22+
return count
23+
24+
25+
input =sys.stdin.readline
26+
N , M , K = map(int, input().split()) # field 범위, 씨앗 개수, 확산량
27+
X = M
28+
# 1. field 와 방문 등록 받기
29+
field = [[0 for _ in range(N)] for k in range(N)]
30+
visited = [[False for _ in range(N)] for k in range(N)]
31+
for i in range(N) :
32+
field[i]=list(map(int, input().split()))
33+
# 상하좌우
34+
dy = [-1,1,0,0]
35+
dx = [0,0, -1,1]
36+
37+
#2. bfs
38+
39+
for i in range(N):
40+
for j in range(N):
41+
#포자 가능 지역 + 방문 안 한곳
42+
if field[i][j] == 0 and not visited[i][j]:
43+
cur_node = bfs( i, j , visited)
44+
# 사용할 씨앗 청구하기
45+
X = X - math.ceil(cur_node/K)
46+
if X < 0 : #씨앗이 음수 남았으면 -> 불가능
47+
print("IMPOSSIBLE")
48+
exit()
49+
# 3. 마무리 출력
50+
if X == M : # 필드 자체가 농사 불가 -> 1개도 사용 못함
51+
print("IMPOSSIBLE")
52+
exit()
53+
elif X >= 0 : # 씨앗 남음
54+
print("POSSIBLE")
55+
print(X)

0 commit comments

Comments
 (0)