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
|
from collections import deque
import sys
dx = [-1,1,0,0]
dy = [0,0,-1,1]
def worm(x,y):
que = deque()
que.append((x,y))
if graph[x][y]== 0:
return False
while que:
x,y = que.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx<0 or ny<0 or nx>=h or ny>=w:
continue
if graph[nx][ny]==0:
continue
if graph[nx][ny]==1:
que.append((nx,ny))
graph[x][y]=0
graph[nx][ny]=0
return True
n = int(input())
for _ in range(n):
w, h, n = map(int, input().split())
graph = [[0 for j in range(w)] for i in range(h)]
count = 0
for i in range(n):
worm1, worm2 = map(int, sys.stdin.readline().split())
graph[worm2][worm1]=1
for x in range(h):
for y in range(w):
if worm(x,y) is True:
count += 1
print(count)
|
cs |
'백준' 카테고리의 다른 글
백준 11866번: 요세푸스 문제 0 (0) | 2021.06.27 |
---|---|
백준 4963번: 섬의 개수 (0) | 2021.05.20 |
백준 11724번: 연결 요소의 개수 (0) | 2021.05.20 |
백준 2606번: 바이러스 (0) | 2021.05.20 |
백준 10773번: 제로 (0) | 2021.05.18 |