https://www.acmicpc.net/problem/2636
2636๋ฒ: ์น์ฆ
์๋ <๊ทธ๋ฆผ 1>๊ณผ ๊ฐ์ด ์ ์ฌ๊ฐํ ์นธ๋ค๋ก ์ด๋ฃจ์ด์ง ์ฌ๊ฐํ ๋ชจ์์ ํ์ด ์๊ณ , ๊ทธ ์์ ์์ ์น์ฆ(ํ์์ผ๋ก ํ์๋ ๋ถ๋ถ)๊ฐ ๋์ฌ ์๋ค. ํ์ ๊ฐ์ฅ์๋ฆฌ(<๊ทธ๋ฆผ 1>์์ ๋ค๋ชจ ์นธ์ X์น ๋ถ๋ถ)์๋ ์น์ฆ๊ฐ ๋
www.acmicpc.net
from collections import deque
col,row = map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(col)]
def bfs(y,x):
q = deque()
q.append((y,x))
visit=[[0]*row for _ in range(col)]
visit[y][x]=1
while q:
ny,nx= q.popleft()
directy = [-1,1,0,0]
directx = [0,0,-1,1]
for i in range(4):
dy = directy[i] + ny
dx = directx[i] + nx
if 0<=dy<col and 0<=dx<row:
if visit[dy][dx] == 0:
#์น์ฆ๊ฐ ์๋ค๋ฉด...
if arr[dy][dx]==1:
visit[dy][dx]=1
#์น์ฆ ๋
น์ด๊ธฐ!
arr[dy][dx]=0
continue
#์น์ฆ๊ฐ ์๋ค๋ฉด...
if arr[dy][dx]==0:
visit[dy][dx]=1
q.append((dy,dx))
#์น์ฆ์ ๊ฐ์๋ฅผ ์ธ์ด๋ณด์!
def count_cheeses():
cnt_cheese=0
for i in range(col):
for j in range(row):
if arr[i][j]==1:
cnt_cheese+=1
return cnt_cheese
time=0 #์น์ฆ๊ฐ ๋ค ๋
น๋ ์๊ฐ
last_cheeses=0 #๋ง์ง๋ง ์น์ฆ
while True:
#์น์ฆ์ ๊ฐ์๋ฅผ ์ธ๋ฌ ๊ฐ๊ธฐ!
cnt = count_cheeses()
#์น์ฆ๊ฐ ์๋ค๋ฉด!
if cnt>0:
bfs(0,0) #bfs์์!
last_cheeses=cnt #์น์ฆ์ ๊ฐฏ์ ๊ฐฑ์
time+=1 #์น์ฆ ๋
น๋ ์๊ฐ +1
#์น์ฆ๊ฐ ์๋ค๋ฉด!
elif cnt==0:
#while๋ฌธ ๋ฉ์ถฐ!
break
#์ถ๋ ฅํ๊ธฐ
print(time)
print(last_cheeses)
'Problem Solving > BAEKJOON' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[baekjoon]python #14606 ํผ์ (Small) (0) | 2022.05.18 |
---|---|
[baekjoon]python #1946 ์ ์ ์ฌ์ (0) | 2022.05.13 |
[baekjoon]python #2469 ์ฌ๋ค๋ฆฌ ํ๊ธฐ (0) | 2022.05.03 |
[baekjoon]python #2606 ๋ฐ์ด๋ฌ์ค (0) | 2022.05.02 |
[baekjoon]python #2589 ๋ณด๋ฌผ์ฌ (0) | 2022.04.26 |
๋๊ธ