(백준) 유기농 양배추 1012호 – PYTHON
https://www.acmicpc.net/problem/1012
1012호: 유기농 양배추
차세대 농부 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 했습니다.
농약을 사용하지 않고 배추를 기르기 위해서는 배추를 병충해로부터 보호하는 것이 중요하기 때문에 한나는 병충해 방제에 심혈을 기울이고 있습니다.
www.acmicpc.net



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