-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1012.cpp
62 lines (54 loc) · 1.04 KB
/
1012.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
int T, M, N, K;
int cnt = 0;
bool Vegeta[51][51];
bool visited[51][51];
int nx[4] = {1,0,-1,0};
int ny[4] = { 0,-1,0,1 };
int mx[4] = { 1,0,-1,0 };
int my[4] = { 0,-1,0,1 };
void dfs(int x, int y) {
visited[x][y] = true;
cnt++;
for (int i = 0; i < 4; i++) {
int nx = x + mx[i];
int ny = y + my[i];
if (0 <= nx && nx < M && 0 <= ny && ny < N) {
if (!visited[nx][ny] && Vegeta[nx][ny]) {
dfs(nx, ny);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int a, b;
cin >> T;
while (T--) {
vector<int> v1;
memset(Vegeta, false, sizeof(Vegeta));
memset(visited, false, sizeof(visited));
cin >> M >> N >> K;
for (int x = 0; x < K; x++) {
cin >> a >> b;
Vegeta[a][b] = true;
}
for (int x = 0; x < M; x++) {
for (int y = 0; y < N; y++) {
if (Vegeta[x][y] && !visited[x][y]) {
cnt = 0;
dfs(x, y);
v1.push_back(cnt);
}
}
}
cout << v1.size() << "\n";
}
return 0;
}