[백준] 2667번 : 단지번호붙이기 [파이썬]

[백준] 2667번 : 단지번호붙이기 [파이썬]

import sys

from collections import deque

input = sys . stdin . readline

n = int ( input ())

graph =[ list ( map ( int , input (). rstrip ())) for _ in range ( n )]

visitied =[[ False ]* n for _ in range ( n )]

dx =[ 1 ,- 1 , 0 , 0 ]

dy =[ 0 , 0 , 1 ,- 1 ]

def bfs ( a , b ):

q = deque ()

q . append (( a , b ))

visitied [ a ][ b ]= True

count = 1

while q :

x , y = q . popleft ()

for i in range ( 4 ):

nx = x + dx [ i ]

ny = y + dy [ i ]

if not ( 0 <= nx < n and 0 <= ny < n ):

continue

if not visitied [ nx ][ ny ]:

visitied [ nx ][ ny ]= True

if graph [ nx ][ ny ]== 1 :

q . append (( nx , ny ))

count += 1

return count

result =[]

for i in range ( n ):

for j in range ( n ):

if graph [ i ][ j ]== 1 and not visitied [ i ][ j ]:

result . append ( bfs ( i , j ))

result . sort ()

print ( len ( result ))

for i in result :

from http://20210916start.tistory.com/157 by ccl(A) rewrite - 2021-10-27 16:26:25