-
2667 단지번호 붙히기코테 대비 python/백준 2022. 11. 3. 18:00
https://www.acmicpc.net/problem/2667
#bfs너비우선 탐색 from collections import deque N = int(input()) #무지성으로 일단 그래프 만들기 graph = [] for _ in range(N): graph.append(list(map(int,input()))) #print(N) #print(graph) def bfs (graph,x,y): #하상좌우 dx = [-1,1,0,0] dy = [0,0,-1,1] #큐 만들고 queue = deque() queue.append((x,y)) graph[x][y] = 0 cnt =1 while queue: x,y = queue.popleft() for i in range(4): nx = x + dx[i] ny = y + dy[i] if nx < 0 or nx>=N or ny< 0 or ny>=N: continue if graph[nx][ny] == 1: graph[nx][ny] = 0 queue.append((nx,ny)) cnt+=1 return cnt count = [] for i in range(N): for j in range(N): if graph[i][j]==1: count.append(bfs(graph,i,j)) count.sort() print(len(count)) for i in range(len(count)): print(count[i])
'코테 대비 python > 백준' 카테고리의 다른 글
1436 (0) 2023.01.21 12919 A와 B2 (0) 2022.11.04 2178_미로탐색 (0) 2022.10.25 2606 바이러스 (0) 2022.09.28 8979 올림픽 (2) 2022.09.16