Coding - Algo/python
[백준] 4963번:섬의 개수 (python 파이썬)
jainn
2021. 2. 16. 01:01
728x90
문제
4963번: 섬의 개수
입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스의 첫째 줄에는 지도의 너비 w와 높이 h가 주어진다. w와 h는 50보다 작거나 같은 양의 정수이다. 둘째 줄부터 h개 줄에는 지도
www.acmicpc.net
소스코드
from collections import deque
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline
def dfs(x, y):
dx = [-1, 0, 1, -1, 0, 1, -1, 0, 1]
dy = [-1, -1, -1, 0, 0, 0, 1, 1, 1]
arr[y][x]=0
for i in range(9):
nowx, nowy = dx[i]+x, dy[i]+y
if 0<=nowx<w and 0<=nowy<h and arr[nowy][nowx]:
dfs(nowx, nowy)
while True:
w, h = map(int, input().split())
if w==0 and h==0:
break
arr = [list(map(int,input().split())) for _ in range(h)]
cnt = 0
for i in range(h):
for j in range(w):
if arr[i][j]==1:
cnt += 1
dfs(j, i)
print(cnt)
반응형