
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
https://www.acmicpc.net/problem/4963
알고리즘 문제 중에서 섬의 개수를 구하는 문제를 풀고 있는데 언제 섬의 개수가 카운트 되는지 잘 모르겠어서 질문 남깁니다ㅜㅜ
import collections
def bfs_island(arr):
dx = -1, 1, 0,0
dy = 0,0,-1,1
# 0, 0이 나올때까지 입력받기
while True:
#너비, 높이
w, h = map(int, input().split())
if w == 0 and h == 0:
break
# 섬 그래프를 담을 arr
arr = []
result = 0
for i in range(h):
arr.append(list(map(int, input().split())))
q = collections.deque()
# 맨 처음 초기값 담기
q.append(arr[0])
result += 1
while q:
x, y = q.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx < 0 or ny < 0 or nx >= w or ny >= h:
continue
if arr[nx][ny] == 1:
q.append((nx, ny))
