-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1012.cpp
More file actions
86 lines (81 loc) · 1.75 KB
/
1012.cpp
File metadata and controls
86 lines (81 loc) · 1.75 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
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
int arr[51][51];
int chk[51][51];
int cnt;
int n,m,t;
int bfs(int a, int b)
{
queue< pair<int,int> >q;
q.push(make_pair(a,b));
while(!q.empty())
{
int y = q.front().first;
int x = q.front().second;
q.pop();
if(y+1<n&&chk[y+1][x]==0&&arr[y+1][x]==1)
{
chk[y+1][x] = cnt;
q.push(make_pair(y+1,x));
}
if(y-1>=0&&chk[y-1][x]==0&&arr[y-1][x]==1)
{
chk[y-1][x] = cnt;
q.push(make_pair(y-1,x));
}
if(x+1<m&&chk[y][x+1]==0&&arr[y][x+1]==1)
{
chk[y][x+1] = cnt;
q.push(make_pair(y,x+1));
}
if(x-1>=0&&chk[y][x-1]==0&&arr[y][x-1]==1)
{
chk[y][x-1] = cnt;
q.push(make_pair(y,x-1));
}
}
return 0;
}
int main(int argc, char** argv)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int k,a,b;
cin>>t;
for (int i = 0; i < t; ++i)
{
cin>>m>>n>>k;
cnt = 0;
for (int j = 0; j < k; ++j)
{
cin>>a>>b;
arr[b][a] = 1;
}
for (int j = 0; j < n; ++j)
{
for (int l = 0; l < m; ++l)
{
if(arr[j][l]==1&&chk[j][l]==0)
{
cnt++;
chk[j][l] = cnt;
bfs(j,l);
}
}
}
for (int j = 0; j < n; ++j)
{
for (int l = 0; l < m; ++l)
{
arr[j][l] = 0;
chk[j][l] = 0;
}
}
cout<<cnt<<"\n";
}
}