[백준] 4963번(섬의 개수)

·2023년 8월 22일

백준 문제풀이

목록 보기
108/159

백준 4963번


최종 제출 코드

import sys
input = sys.stdin.readline
sys.setrecursionlimit(50*50)

def solution(row, col):

  global w, h
  
  if row<0 or row>=h or col<0 or col>=w: return
  if array[row][col] == 0: return
  if visited[row][col] == True: return

  visited[row][col] = True

  solution(row+1, col)
  solution(row, col+1)
  solution(row-1, col)
  solution(row, col-1)
  
  # 대각선
  solution(row+1, col+1)
  solution(row-1, col-1)
  solution(row+1, col-1)
  solution(row-1, col+1)


while True:
  w, h = map(int, input().split())
  if w==h==0: break
    
  array = [list(map(int, input().split())) for _ in range(h)]
  visited = [[False for _ in range(w)] for _ in range(h)]

  cnt = 0
  
  for i in range(h):
    for j in range(w):
      if array[i][j] == 1 and visited[i][j] == False:
        solution(i, j)
        cnt += 1

  print(cnt)

◼️ 단지번호붙이기와 유사한 문제

  • 같은 범위로 간주하는 블록에 대각선도 포함된다는 점만 다름
profile
백엔드 개발자가 되고 싶어요(22.8.15~)

0개의 댓글