from collections import deque
t=int(input())
dx=[1,-1,0,0]
dy=[0,0,1,-1]
def bfs(graph,x,y):
graph[x][y]=0
queue=deque()
queue.append((x,y))
while queue:
x,y = queue.popleft()
for i in range(4):
nx=x+dx[i]
ny=y+dy[i]
if nx>=n or nx<0 or ny<0 or ny>=m:
continue
if graph[nx][ny]==1:
queue.append((nx,ny))
graph[nx][ny]=0
return
for _ in range(t):
cnt=0
n,m,num=map(int,input().split())
graph=[[0]*m for x in range(n)]
for i in range(num):
x,y=map(int,input().split())
graph[x][y]=1
for i in range(n):
for j in range(m):
if graph[i][j]:
bfs(graph,i,j)
cnt+=1
print(cnt)
'백준 풀이' 카테고리의 다른 글
백준 1697번 파이썬 bfs (0) | 2022.08.28 |
---|---|
백준 1931 파이썬 (0) | 2022.08.28 |
백준 1149번 파이썬 dp (0) | 2022.08.28 |
백준 1260번 파이썬 dfs, bfs 개념 (0) | 2022.08.28 |
백준 2667번 파이썬 bfs, dfs (0) | 2022.08.28 |