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
| #include <bits/stdc++.h>
using namespace std;
int main() { int n; cin >> n; vector<vector<vector<bool>>> jellies(n, vector<vector<bool>>(n, vector<bool>(n, false))); string s; for (int z = 0; z < n; z++) for (int x = 0; x < n; x++) { cin >> s; for (int y = 0; y < n; y++) if (s[y] == '.') jellies[z][x][y] = true; } int ds[] = {0, 0, 1, 0, 0, -1, 0, 0}, steps = 1; queue<tuple<int, int, int>> q; q.emplace(0, 0, 0); jellies[0][0][0] = false; while (!q.empty()) { int size = q.size(); while (size--) { auto[z, x, y] = q.front(); q.pop(); if (z == n - 1 && x == n - 1 && y == n - 1) { cout << steps << endl; return 0; } for (int i = 0; i < 6; ++i) { int nz = z + ds[i], nx = x + ds[i + 1], ny = y + ds[i + 2]; if (nz < 0 || nz > n - 1 || nx < 0 || nx > n - 1 || ny < 0 || ny > n - 1 || !jellies[nz][nx][ny]) continue; jellies[nz][nx][ny] = false; q.emplace(nz, nx, ny); } } ++steps; } cout << -1 << endl; return 0; }
|