-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1012.cpp
More file actions
61 lines (50 loc) · 1.12 KB
/
1012.cpp
File metadata and controls
61 lines (50 loc) · 1.12 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
#include <iostream>
#include <vector>
using namespace std;
vector<vector<bool>> field;
vector<vector<bool>> visited;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int N, M;
void dfs(int x, int y)
{
visited[x][y] = true;
for (int dir = 0; dir < 4; dir++)
{
int tox = x + dx[dir];
int toy = y + dy[dir];
if (tox < 0 || toy < 0 || tox >= M || toy >= N || !field[tox][toy] || visited[tox][toy])
continue;
dfs(tox, toy);
}
}
int main()
{
int T, K, a, b;
cin >> T;
while (T--)
{
int answer = 0;
cin >> M >> N >> K;
field.assign(M, vector<bool>(N, false));
visited.assign(M, vector<bool>(N, false));
while (K--)
{
cin >> a >> b;
field[a][b] = true;
}
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
if (field[i][j] && !visited[i][j])
{
dfs(i, j);
answer++;
}
}
}
cout << answer << endl;
}
return 0;
}