-
Notifications
You must be signed in to change notification settings - Fork 0
/
1012.cpp
52 lines (49 loc) · 1.24 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
#include <iostream>
#include <queue>
using namespace std;
int board[52][52];
bool check[52][52];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int test,n,m,k;
void bps(int i, int j){
queue<pair <int,int> > Q;
Q.push(make_pair(i,j));
while(!Q.empty()){
pair<int,int> cur = Q.front();
Q.pop();
for(int i=0; i<4; i++){
int nx = cur.first + dx[i];
int ny = cur.second + dy[i];
if(nx <0 || nx >= n || ny < 0 || ny >= m) continue;
if(board[nx][ny] == 0 || check[nx][ny] == 1) continue;
check[nx][ny] = 1;
Q.push(make_pair(nx,ny));
}
}
}
int main(){
cin >> test;
while(test--){
cin >> n >> m >> k;
for(int i=0; i<k; i++){
int a,b;
cin >> a >> b;
board[a][b] =1;
}
int count = 0;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(board[i][j] == 1 && check[i][j] == 0){
bps(i,j);
count++;
}
}
}
cout << count << '\n';
for(int i=0; i<n; i++){
fill(board[i],board[i]+m,0);
fill(check[i],check[i]+m,0);
}
}
}