hgk0404.tistory
Code After Work
hgk0404.tistory

공지사항

  • 블로그
전체 방문자
오늘
어제
  • 전체 카테고리
    • [컴퓨터비전]
    • [MLOps]
      • [FastAPI]
    • [Computer Science]
      • [컴퓨터네트워크]
      • [알고리즘]
      • [자료구조 in C]
      • [C & C++]
      • [이산수학]
      • [Math]
    • [머신러닝]
      • [Numpy, Pandas]
    • [Cloud]
      • [AWS]
      • [NCP]
      • [Kubernetes]
      • [Terraform]
    • [Dev]
      • [가상환경]
      • [Linux]
      • [Docker]
    • [Python]
    • [Coding Test]
      • [백준]
      • [프로그래머스]
      • [SQL]
    • [WEB]
    • [일상]
    • [엑셀]
    • [금융]

인기 글

최근 글

최근 댓글

250x250
hELLO · Designed By 정상우.
hgk0404.tistory

Code After Work

[백준] 2667: 단지번호붙이기
[Coding Test]/[백준]

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

2022. 6. 29. 23:01
728x90

https://www.acmicpc.net/problem/2667

 

 

from collections import deque
n = int(input())
graph = [ list(map(int, input())) for _ in range(n) ]
dx = [ -1, 1, 0, 0 ]
dy = [ 0, 0, -1, 1 ]
answer = []
cnt = 0 #1
res = 0 #2

def bfs(x, y):
    global cnt
    graph[x][y] = 2 #3
    q = deque()
    q.append((x,y)) #4
    
    while q:
        x, y = q.popleft()
        cnt += 1 #5
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx < n and 0 <= ny < n:
                if graph[nx][ny] == 1:
                    graph[nx][ny] = 2
                    q.append((nx, ny))
    return cnt #6

for i in range(n):
    for j in range(n):
        if graph[i][j] == 1: #7
            answer.append(bfs(i, j))
            res += 1
            cnt = 0 

answer.sort()
print(res)
for i in answer:
    print(i)

 

728x90
저작자표시 동일조건 (새창열림)

'[Coding Test] > [백준]' 카테고리의 다른 글

[백준] 1697: 숨바꼭질  (0) 2022.06.30
[백준] 7569: 토마토  (0) 2022.06.30
[백준] 1182: 부분수열의 합  (0) 2022.06.28
[백준] 2178: 미로 탐색  (0) 2022.06.27
[백준] 7568: 덩치  (0) 2022.06.24
'[Coding Test]/[백준]' 카테고리의 다른 글
  • [백준] 1697: 숨바꼭질
  • [백준] 7569: 토마토
  • [백준] 1182: 부분수열의 합
  • [백준] 2178: 미로 탐색
hgk0404.tistory
hgk0404.tistory
공부기록

티스토리툴바