Skip to content

Commit dd124de

Browse files
committed
[BOJ] 유기농 배추 / 실버1 / 60분
https://www.acmicpc.net/problem/1012
1 parent e84d69e commit dd124de

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
T = int(input())
2+
3+
dx = [-1,1,0,0]
4+
dy = [0,0,-1,1]
5+
6+
def BFS(x,y):
7+
queue = [(x,y)]
8+
matrix[x][y] = 0
9+
10+
while queue:
11+
x,y = queue.pop(0)
12+
13+
for i in range(4):
14+
nx = x + dx[i]
15+
ny = y + dy[i]
16+
17+
if nx < 0 or nx >= M or ny < 0 or ny >= N:
18+
continue
19+
20+
if matrix[nx][ny] == 1 :
21+
queue.append((nx,ny))
22+
matrix[nx][ny] = 0
23+
24+
for i in range(T):
25+
M, N, K = map(int,input().split())
26+
matrix = [[0]*(N) for _ in range(M)]
27+
cnt = 0
28+
29+
for j in range(K):
30+
x,y = map(int, input().split())
31+
matrix[x][y] = 1
32+
33+
for a in range(M):
34+
for b in range(N):
35+
if matrix[a][b] == 1:
36+
BFS(a,b)
37+
cnt += 1
38+
39+
print(cnt)

0 commit comments

Comments
 (0)