-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1012.cpp
More file actions
74 lines (63 loc) · 1.32 KB
/
1012.cpp
File metadata and controls
74 lines (63 loc) · 1.32 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
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define vt vector
#define ll long long
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
#define en '\n'
const int d4r[4] = {-1,0,1,0}; const int d4c[4] = {0,1,0,-1};
const int d8r[8] = {-1,-1,0,1,1,1,0,-1}; const int d8c[8] = {0,1,1,1,0,-1,-1,-1};
const int INF = 0x3f3f3f3f; const int mINF = 0xc0c0c0c0;
const ll LINF = 0x3f3f3f3f3f3f3f3f; const ll mLINF = 0xc0c0c0c0c0c0c0c0;
int T = 1;
int N,M,K;
int bd[51][51];
bool v[51][51];
int ans;
void sol() {
cin >> M >> N >> K;
memset(bd,0,sizeof(bd));
memset(v,0,sizeof(v));
ans = 0;
int r,c;
for(int i=0; i<K; ++i) {
cin >> c >> r;
bd[r][c] = 1;
}
for(int i=0; i<N; ++i) {
for(int j=0; j<M; ++j) {
if(bd[i][j] && !v[i][j]) {
queue<pii> q;
q.push({i,j});
v[i][j] = 1;
ans++;
while(!q.empty()) {
pii p = q.front(); q.pop();
r = p.first;
c = p.second;
int nr,nc;
for(int d=0; d<4; ++d) {
nr = r + d4r[d];
nc = c + d4c[d];
if(0<=nr&&nr<N && 0<=nc&&nc<M && bd[nr][nc] && !v[nr][nc]) {
v[nr][nc] = 1;
q.push({nr,nc});
}
}
}
}
}
}
cout << ans << en;
return;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> T;
while(T--) {
sol();
}
return 0;
}