-
Notifications
You must be signed in to change notification settings - Fork 0
/
1012.cpp
71 lines (57 loc) · 1.17 KB
/
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
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
#include<queue>
using namespace std;
int board[50][50];
int visit[50][50];
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
int row, col;
int count = 0;
void bfs( int i, int j) {
if (visit[i][j] == 1)
return;
else {
//cout << i << " " << j << endl;
queue<pair<int, int>>q;
q.push({ i,j });
visit[i][j] = 1;
while (!q.empty()) {
pair<int, int> cur = q.front(); q.pop();
for (int i = 0; i < 4; i++) {
int nx = cur.first + dx[i];
int ny = cur.second + dy[i];
if (nx < 0 || nx >= col || ny < 0 || ny >= row)
continue;
if (visit[nx][ny] == 1 || board[nx][ny] == 0)
continue;
visit[nx][ny] = 1;
q.push({ nx,ny });
}
}
::count++;
}
}
int main() {
int repet, x,y,k;
cin >> repet;
for (int i = 0; i < repet; i++) {
cin >> row >> col >> k;
for (int j = 0; j < k; j++) {
cin >> x >> y;
board[y][x] = 1;
}
for (int j = 0; j < col; j++) {
for (int t = 0; t < row; t++) {
if (board[j][t] == 1) {
bfs(j, t);
}
}
}
cout << ::count << endl;
::count = 0;
for (int j = 0; j < col; j++) {
fill(board[j], board[j] + row, 0);
fill(visit[j], visit[j] + row, 0);
}
}
}