-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7569.cpp
More file actions
73 lines (64 loc) · 1.49 KB
/
7569.cpp
File metadata and controls
73 lines (64 loc) · 1.49 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
#include <iostream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
int arr[100][100][100];
vector<pair<int, pair<int, int>>>v;
int dx[6] = { 0, 1, -1, 0,0, 0};
int dy[6] = { 1, 0, 0,-1, 0, 0};
int dz[6] = { 0,0,0,0,1, -1};
bool visit[100][100][100];
int main() {
int m, n, h;
cin >> m >> n >> h;
for (int i = 0; i < h; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
cin >> arr[i][j][k];
if (arr[i][j][k] == 1) {
v.push_back({ i,{ j,k } });
}
}
}
}
queue<pair<pair<int,int>, pair<int, int>>>q;
for (pair<int, pair<int, int>> p : v) {
int i = p.first;
int j = p.second.first;
int k = p.second.second;
q.push({ {0, i}, {j,k} });
visit[i][j][k] = true;
}
int t=0;
while (!q.empty()) {
t= q.front().first.first;
int z = q.front().first.second;
int x = q.front().second.first;
int y = q.front().second.second;
q.pop();
for (int i = 0; i < 6; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
int nz = z + dz[i];
if (nx < 0 || ny < 0 || nz < 0 || nx >= n || ny >= m || nz >= h) continue;
if (visit[nz][nx][ny]) continue;
if (arr[nz][nx][ny] == 1 || arr[nz][nx][ny] == -1) continue;
visit[nz][nx][ny] = true;
arr[nz][nx][ny] = 1;
q.push({ { t + 1, nz }, { nx,ny }});
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
if (arr[i][j][k] == 0) {
cout << "-1";
return 0;
}
}
}
}
cout << t;
return 0;
}