-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1012유기농배추.py
More file actions
33 lines (30 loc) · 784 Bytes
/
1012유기농배추.py
File metadata and controls
33 lines (30 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import sys
input = sys.stdin.readline
from collections import deque
dx=[0,1,0,-1]
dy=[1,0,-1,0]
def BFS(x,y):
queue=deque()
queue.append([x,y])
radish[x][y]=0
while queue:
x,y=queue.popleft()
for i in range(4):
nx=x+dx[i]
ny=y+dy[i]
if 0<=nx<N and 0<=ny<M and radish[nx][ny]:
radish[nx][ny]=0
queue.append([nx,ny])
for test in range(1,int(input())+1):
M,N,K = map(int,input().split())
radish = [[0 for _ in range(M)] for _ in range(N)]
for _ in range(K):
x,y=map(int,input().split())
radish[y][x]=1
cnt=0
for i in range(N):
for j in range(M):
if radish[i][j]==1:
BFS(i,j)
cnt+=1
print(cnt)