-
Notifications
You must be signed in to change notification settings - Fork 0
/
1012.cpp
60 lines (57 loc) · 990 Bytes
/
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
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
bool paper[51][51];
bool visit[51][51];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
void dfs(int y, int x)
{
for(int i=0;i<4;i++)
{
int nx = x+dx[i], ny = y+dy[i];
if(ny>=0 && nx >=0 && ny<51 && nx<51 && !visit[ny][nx] && paper[ny][nx])
{
visit[ny][nx] = true;
dfs(ny, nx);
}
}
}
int main()
{
int t;
scanf(" %d", &t);
for(int q=0;q<t;q++)
{
int wcnt = 0;
int n, m, y, x;
scanf("%d %d", &m, &n);
int b;
scanf(" %d", &b);
for(int j=0;j<b;j++)
{
scanf("%d %d", &x, &y);
paper[y][x] = true;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(!visit[i][j] && paper[i][j])
{
visit[i][j] = true;
dfs(i, j);
wcnt++;
}
}
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
visit[i][j] = false;
paper[i][j] = false;
}
printf("%d\n", wcnt);
}
return 0;
}