-
Notifications
You must be signed in to change notification settings - Fork 0
/
boj_01012.py
41 lines (34 loc) · 976 Bytes
/
boj_01012.py
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
41
# 2022.03.14
# Dongyoung Kwon @Chuncheonian (ehddud2468@gmail.com)
# https://www.acmicpc.net/problem/1012
from collections import deque
def bfs(i, j, graph):
queue = deque()
queue.append((i, j))
graph[i][j] = 0
while queue:
x, y = queue.popleft()
for k in range(4):
nx = x + dr[k]
ny = y + dc[k]
if nx < 0 or nx >= m or ny < 0 or ny >= n:
continue
if graph[nx][ny] == 1:
graph[nx][ny] = 0
queue.append((nx, ny))
t = int(input())
dr = [0, -1, 0, 1]
dc = [1, 0, -1, 0]
for _ in range(t):
m, n, k = map(int, input().split())
matrix = [[0]*n for _ in range(m)]
count = 0
for _ in range(k):
i, j = map(int, input().split())
matrix[i][j] = 1
for x in range(m):
for y in range(n):
if matrix[x][y] == 1:
bfs(x, y, matrix)
count += 1
print(count)