-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1012유기농배추.py
More file actions
40 lines (32 loc) · 922 Bytes
/
1012유기농배추.py
File metadata and controls
40 lines (32 loc) · 922 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
34
35
36
37
38
39
40
import sys
sys.setrecursionlimit(10000)
from collections import deque
input = sys.stdin.readline
dx = [1,-1,0,0]
dy = [0,0,-1,1]
t = int(input())
def bfs(visited,ary):
q = deque([[i,j]])
while q:
x,y = q.popleft()
visited[x][y]=1
for idx in range(4):
X = x + dx[idx]
Y = y + dy[idx]
if 0<=X<n and 0<=Y<m and not visited[X][Y] and ary[X][Y]==1:
if [X,Y] not in q:
q.append([X,Y])
for _ in range(t):
m,n,k = map(int, input().split())
ary = [[0 for i in range(m)] for j in range(n)]
for __ in range(k):
y,x = map(int,input().split())
ary[x][y]=1
visited = [[0 for i in range(m)] for j in range(n)]
cnt = 0
for i in range(n):
for j in range(m):
if not visited[i][j] and ary[i][j]==1:
bfs(visited,ary)
cnt+=1
print(cnt)