-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1091.cpp
More file actions
68 lines (59 loc) · 1.6 KB
/
1091.cpp
File metadata and controls
68 lines (59 loc) · 1.6 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
#include <iostream>
#include <queue>
using namespace std;
int m, n, l, t;
int pixels[1300][130][65];
bool marked[1300][130][65];
struct Position {
int x, y, z;
friend Position operator+(const Position &a, const Position &b) {
return Position{a.x + b.x, a.y + b.y, a.z + b.z};
}
bool is_legal() {
if (x >= m || x < 0) return false;
if (y >= n || y < 0) return false;
if (z >= l || z < 0) return false;
if (!pixels[x][y][z]) return false;
if (marked[x][y][z]) return false;
return true;
}
void mark() { marked[x][y][z] = true; }
};
Position moves[] {
Position{+1, 0, 0}, Position{-1, 0, 0},
Position{0, +1, 0}, Position{0, -1, 0},
Position{0, 0, +1}, Position{0, 0, -1},
};
int bfs(Position pos) {
if (!pos.is_legal()) return 0;
int count = 0;
queue<Position> q;
q.push(pos);
pos.mark();
while (!q.empty()) {
Position tmp = q.front(); q.pop();
count++;
for (auto move : moves) {
Position next = tmp + move;
if (next.is_legal()) {
q.push(next);
next.mark();
}
}
}
return count >= t ? count : 0;
}
int main() {
cin >> m >> n >> l >> t;
for (int k = 0; k < l; k++)
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
cin >> pixels[i][j][k];
int ans = 0;
for (int k = 0; k < l; k++)
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
ans += bfs(Position{i, j, k});
cout << ans << endl;
return 0;
}