(백준) 유기농 양배추 1012호 – PYTHON
https://www.acmicpc.net/problem/1012
T = int(input()) #테스트케이스의 개수
dx = (-1,1,0,0)
dy = (0,0,-1,1)
def BFS(x,y):
queue = ((x,y))
matrix(x)(y) = 0 # 방문처리
while queue:
x,y = queue.pop(0)
for i in range(4):
nx = x + dx(i)
ny = y + dy(i)
if nx < 0 or nx >= M or ny < 0 or ny >= N:
continue
if matrix(nx)(ny) == 1 :
queue.append((nx,ny))
matrix(nx)(ny) = 0
# 행렬만들기
for i in range(T):
M, N, K = map(int,input().split())
matrix = ((0)*(N) for _ in range(M))
cnt = 0
for j in range(K):
x,y = map(int, input().split())
matrix(x)(y) = 1
for a in range(M):
for b in range(N):
if matrix(a)(b) == 1:
BFS(a,b)
cnt += 1
print(cnt)
>1012_OrganicCabbage.py
1
5 3 6
0 2
1 2
2 2
3 2
4 2
4 0
2