|
| 1 | +#include <string> |
| 2 | +#include <vector> |
| 3 | +#include <iostream> |
| 4 | +#include <queue> |
| 5 | +#include <stack> |
| 6 | +#include <algorithm> |
| 7 | + |
| 8 | +using namespace std; |
| 9 | + |
| 10 | +int dx[6] = {0, 0, 1, -1, 0, 0}; |
| 11 | +int dy[6] = {1, -1, 0, 0, 0, 0}; |
| 12 | +int dz[6] = {0, 0, 0, 0, 1, -1}; |
| 13 | +int days[101][101][101]; |
| 14 | +int boards[101][101][101]; |
| 15 | + |
| 16 | +int main() { |
| 17 | + ios::sync_with_stdio(0); |
| 18 | + cin.tie(0); |
| 19 | + for (int i=0;i<101;i++) { |
| 20 | + for (int j=0;j<101;j++) { |
| 21 | + fill(days[i][j], days[i][j]+101, -1); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + int M, N, H; |
| 26 | + queue<tuple<int, int, int> > Q; |
| 27 | + cin >> M >> N >> H; |
| 28 | + for (int h=0;h<H;h++) { |
| 29 | + for (int i=0;i<N;i++) { |
| 30 | + for (int j=0;j<M;j++) { |
| 31 | + cin >> boards[h][i][j]; |
| 32 | + if (boards[h][i][j] == 1) { |
| 33 | + Q.push(make_tuple(j, i, h)); |
| 34 | + days[h][i][j] = 0; |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + while (!Q.empty()) { |
| 40 | + tuple<int, int, int> top = Q.front(); Q.pop(); |
| 41 | + for (int i=0;i<6;i++) { |
| 42 | + int nx = get<0>(top) + dx[i]; |
| 43 | + int ny = get<1>(top) + dy[i]; |
| 44 | + int nz = get<2>(top) + dz[i]; |
| 45 | + if (0 <= nx && nx < M && 0 <= ny && ny < N && 0 <= nz && nz < H && boards[nz][ny][nx] != -1 && days[nz][ny][nx] == -1) { |
| 46 | + days[nz][ny][nx] = days[get<2>(top)][get<1>(top)][get<0>(top)] + 1; |
| 47 | + Q.push(make_tuple(nx, ny, nz)); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + int result = 0; |
| 52 | + for (int h=0;h<H;h++) { |
| 53 | + for (int i=0;i<N;i++) { |
| 54 | + for (int j=0;j<M;j++) { |
| 55 | + if (boards[h][i][j] != -1 && days[h][i][j] == -1) { |
| 56 | + cout << -1; |
| 57 | + return 0; |
| 58 | + } else if (boards[h][i][j] != -1) { |
| 59 | + result = max(result, days[h][i][j]); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + cout << result; |
| 65 | + |
| 66 | + |
| 67 | + return 0; |
| 68 | +} |
0 commit comments