✔ 풀이를 위한 아이디어
✔ 코드
import sys
from collections import deque
T = int(sys.stdin.readline())
for _ in range(T):
M, N, K = map(int, sys.stdin.readline().split())
box = [0] * (M*N)
queue = deque([])
stack = []
for i in range(K):
x, y = map(int, sys.stdin.readline().split())
current = (M * y) + x
box[current] = 1
queue.append(current)
count = 0
while queue:
tmp = queue.popleft()
if box[tmp] == 1:
stack.append(tmp)
count += 1
while stack:
tmp = stack.pop()
if tmp % M > 0:
if box[tmp-1] == 1:
stack.append(tmp-1)
box[tmp-1] = 0
if (tmp+1) % M > 0:
if box[tmp+1] == 1:
stack.append(tmp+1)
box[tmp+1] = 0
if tmp >= M:
if box[tmp-M] == 1:
stack.append(tmp-M)
box[tmp-M] = 0
if tmp < M*(N-1):
if box[tmp+M] == 1:
stack.append(tmp+M)
box[tmp+M] = 0
print(count)
✔ 관련 개념