BOJ - 14716 - 현수막
Updated:
import sys
from collections import deque
dx = [0, 0, 1, -1, 1, -1, 1, -1]
dy = [1, -1, 0, 0, 1, -1, -1, 1]
def BFS(maps, M, N, x, y):
check = [[-1 for _ in range(N)] for _ in range(M)]
dq = deque()
dq.append((x, y))
while dq:
tx, ty = dq.popleft()
for i in range(8):
nx = tx + dx[i]
ny = ty + dy[i]
if 0 <= nx < M and 0 <= ny < N:
if maps[nx][ny] == 1:
dq.append((nx, ny))
maps[nx][ny] = 0
def solution():
M, N = map(int, sys.stdin.readline().split())
maps = []
for _ in range(M):
maps.append(list(map(int, sys.stdin.readline().split())))
cnt = 0
for i in range(M):
for j in range(N):
if maps[i][j] == 1:
cnt += 1
BFS(maps, M, N, i, j)
print(cnt)
solution()
https://www.acmicpc.net/problem/14716
Leave a comment