-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1012.cpp
More file actions
63 lines (58 loc) · 1.47 KB
/
1012.cpp
File metadata and controls
63 lines (58 loc) · 1.47 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
60
61
62
63
#include <iostream>
#include <queue>
#include <vector>
#include <string.h>
#include <algorithm>
using namespace std;
int m, n, k;
int arr[51][51];
int visited[51][51];
int dy[4]={0, 0, 1, -1};
int dx[4]={1, -1, 0, 0};
void bfs(int y, int x){
queue<pair<int, int> > q;
q.push(make_pair(y,x));
visited[y][x]=1;
while(!q.empty()){
int cur_y = q.front().first;
int cur_x = q.front().second;
q.pop();
for(int i=0; i<4; i++){
int next_y = cur_y + dy[i];
int next_x = cur_x + dx[i];
if(next_y<0 || next_x<0 || next_y>=n || next_x>=m) continue;
if(arr[next_y][next_x]==1){
if(visited[next_y][next_x]==0){
visited[next_y][next_x]=1;
q.push(make_pair(next_y, next_x));
}
}
}
}
}
int main(){
int t;
cin>>t;
while(t--){
memset(arr, false, sizeof(arr));
memset(visited, false, sizeof(visited));
int result=0;
cin>>m>>n>>k;
for(int i=0; i<k; i++){
int a, b;
cin>>a>>b;
arr[b][a]=1;
}
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(arr[i][j]==1 && visited[i][j]==0){
//cout<<i<<","<<j<<endl;
bfs(i,j);
result++;
}
}
}
cout<<result<<endl;
}
return 0;
}