-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1012.cpp
More file actions
98 lines (87 loc) · 2.22 KB
/
1012.cpp
File metadata and controls
98 lines (87 loc) · 2.22 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
87
88
89
90
91
92
93
94
95
96
97
98
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int field[51][51];
#define Y first
#define X second
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--) {
int N, M, K;
cin >> N >> M >> K;
for (int i = 0; i < K; i++) {
int y, x;
cin >> y >> x;
field[y][x] = 1;
}
int bug = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (field[i][j] == 1) {
bug++;
field[i][j] = 2;
queue<pair<int, int>> Q;
Q.push(make_pair(i, j));
while (!Q.empty()) {
auto cur = Q.front();
Q.pop();
for (int dir = 0; dir < 4; dir++) {
int ny = cur.Y + dy[dir];
int nx = cur.X + dx[dir];
if (ny < 0 || ny >= N || nx < 0 || nx >= M)
continue;
if (field[ny][nx] == 1) {
Q.push(make_pair(ny, nx));
field[ny][nx] = 2;
}
}
}
}
}
}
cout << bug << '\n';
}
}
/*
#include <bits/stdc++.h>
using namespace std;
int map[51][51];
int N, M, K;
void BFS(int p, int q) {
if (map[p][q] != 1 || p<0 || p>=N || q<0 || q>=M) return;
map[p][q] = 2;
BFS(p - 1, q);
BFS(p, q - 1);
BFS(p, q + 1);
BFS(p + 1, q);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T, X, Y;
int wiggler;
cin >> T;
while(T--) {
cin >> M >> N >> K;
for (int i = 0; i < K; i++) {
cin >> X >> Y;
map[Y][X] = 1;
}
wiggler = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (map[i][j] == 1) {
wiggler++;
BFS(i, j);
}
}
}
cout << wiggler << endl;
}
return 0;
}
*/