-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1012.cpp
More file actions
59 lines (52 loc) · 1.27 KB
/
1012.cpp
File metadata and controls
59 lines (52 loc) · 1.27 KB
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
#include <iostream>
using namespace std;
int nextX[4] = {1, 0, -1, 0};
int nextY[4] = {0, 1, 0, -1};
int n, m;
bool **arr;
void DFS(int currX, int currY){
arr[currY][currX] = false;
for(int i = 0; i < 4; i++){
if(currY + nextY[i] < 0) continue;
if(currY + nextY[i] >= n) continue;
if(currX + nextX[i] < 0) continue;
if(currX + nextX[i] >= m) continue;
if(arr[currY + nextY[i]][currX + nextX[i]] == true){
DFS(currX + nextX[i], currY + nextY[i]);
}
}
return;
}
int main(){
int t;
cin >> t;
while(t){
int k;
cin >> m >> n >> k;
arr = new bool*[n];
for(int i = 0; i < n; i++){
arr[i] = new bool[m]();
}
for(int i = 0; i < k; i++){
int x, y;
cin >> x >> y;
arr[y][x] = true;
}
int ans = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(arr[i][j] == true){
ans++;
DFS(j, i);
}
}
}
cout << ans << endl;
for(int i = 0; i < n; i++){
delete[] arr[i];
}
delete[] arr;
t--;
}
return 0;
}