[백준] 4963번 : 섬의 개수 - Python(파이썬)

강재원·2022년 11월 24일
0

[코딩테스트] Python

목록 보기
190/200
post-custom-banner



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

import sys
sys.setrecursionlimit(10000)
dx=[0,0,1,-1,1,1,-1,-1]
dy=[1,-1,0,0,1,-1,-1,1]

def find(a,b):
    check[a][b]=True
    for k in range(8):
        x=a+dx[k]
        y=b+dy[k]
        if x>=0 and x<h and y>=0 and y<w:
            if arr[x][y]==1 and check[x][y]==False: find(x,y)

while True:
    w,h=map(int,input().split())
    if w==0 and h==0: break
    arr=[[0 for _ in range(w)] for _ in range(h)]
    for i in range(h):
        st=list(map(int,input().split()))
        for j in range(w):
            arr[i][j]=st[j]
    check = [[False for _ in range(w)] for _ in range(h)]
    count=0

    for i in range(h):
        for j in range(w):
            if arr[i][j]==1 and check[i][j]==False:
                find(i,j)
                count+=1
    print(count)
profile
개념정리 & 문법 정리 & 알고리즘 공부
post-custom-banner

0개의 댓글