-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathback_1012.cpp
More file actions
72 lines (64 loc) · 1.29 KB
/
back_1012.cpp
File metadata and controls
72 lines (64 loc) · 1.29 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
#include <iostream>
#include <algorithm>
using namespace std;
int t,m,n,b;
int visted[51][51];
int map[51][51];
int drx[4] = {0, 0, 1, -1};
int dry[4] = {1, -1,0, 0};
void reset()
{
for (int yy = 0; yy < 51; yy++)
{
for (int xx = 0; xx < 51; xx++)
{
map[yy][xx] = 0;
visted[yy][xx] = 0;
}
}
}
void dfs(int y,int x)
{
visted[y][x] = true;
for (int i = 0; i < 4; i++)
{
int dy = dry[i] + y;
int dx = drx[i] + x;
if (dy >= 0 && dx >= 0 && dx < m && dy < n)
{
if (visted[dy][dx] == 0 && map[dy][dx] == 1)
dfs(dy, dx);
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> t;
for (int i = 0; i< t; i++)
{
int sum = 0;
cin >> m >> n >> b;
for (int i = 0; i < b; i++)
{
int x, y;
cin >> x >> y;
map[y][x] = 1;
}
for (int yy = 0; yy < n; yy++)
{
for (int xx = 0; xx < m; xx++)
{
if (map[yy][xx] == 1 && !visted[yy][xx])
{
sum++;
dfs(yy,xx);
}
}
}
cout << sum << "\n";
reset();
}
return 0;
}