-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1012.cpp
More file actions
44 lines (38 loc) · 766 Bytes
/
1012.cpp
File metadata and controls
44 lines (38 loc) · 766 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
41
42
43
44
#include<iostream>
#define SIZE 51
int arr[SIZE][SIZE];
int x[4] = { 1,0,-1,0 };
int y[4] = { 0,1,0,-1 };
int N, M, K;
void dfs(int h_x, int h_y) {
arr[h_x][h_y] = false;
for (int i = 0; i < 4; i++) {
int n_x = h_x + x[i];
int n_y = h_y + y[i];
if (n_x < 0 || n_y < 0 || n_x >= N || n_y >= M)
continue;
if (arr[n_x][n_y] == false)
continue;
dfs(n_x, n_y);
}
}
int main(void) {
int T; scanf("%d", &T);
for (int t = 1; t <= T; t++) {
scanf("%d %d %d", &N, &M, &K);
int cnt = 0;
for (int i = 0; i < K; i++) {
int n, m; scanf("%d %d", &n, &m);
arr[n][m] = true;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (arr[i][j] == true) {
cnt++;
dfs(i, j);
}
}
}
printf("%d\n", cnt);
}
}