https://www.acmicpc.net/problem/2667
2667번: 단지번호붙이기
<그림 1>과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. 여
www.acmicpc.net
✔ 소스코드
import sys
N = int(sys.stdin.readline())
graph = [list(input().rstrip()) for i in range(N)]
visited = [[0] * N for i in range(N)]
houses = []
house = 0
def search(i, j):
global house
if i < 0 or j >= N or i >= N or j < 0 or graph[i][j] == '0':
return
graph[i][j] = '0'
visited[i][j] = 1
house += 1
search(i + 1, j)
search(i, j + 1)
search(i - 1, j)
search(i, j - 1)
for i in range(N):
for j in range(N):
if visited[i][j] == 0 and graph[i][j] == '1':
search(i,j)
houses.append(house)
house = 0
print(len(houses))
for i in sorted(houses):
print(i)
'백준문제풀이' 카테고리의 다른 글
[백준 문제풀이] 1697번 : 숨바꼭질 (0) | 2021.09.17 |
---|---|
[백준 문제풀이] 2178번 : 미로탐색 (0) | 2021.09.01 |
[백준 문제풀이] 11724번 : 연결 요소의 개수 (0) | 2021.08.28 |
[백준 문제풀이] 1260번 : DFS와 BFS (0) | 2021.08.24 |
[백준 문제풀이] 13023번 : ABCDE (0) | 2021.08.24 |