fork download
  1. #include <queue>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. struct pos {
  6. int x;
  7. int y;
  8. int z;
  9. };
  10.  
  11. int n, m, k;
  12. queue<pos> q;
  13. int arr[105][105][105];
  14. int map[105][105][105];
  15.  
  16. pos make_pos(int x, int y, int z) {
  17. pos return_pos = {x, y, z};
  18. return return_pos;
  19. }
  20.  
  21. pos change(pos tmp, int i) {
  22. if (i == 1) return make_pos(tmp.x - 1, tmp.y, tmp.z);
  23. else if (i == 2) return make_pos(tmp.x + 1, tmp.y, tmp.z);
  24. else if (i == 3) return make_pos(tmp.x, tmp.y - 1, tmp.z);
  25. else if (i == 4) return make_pos(tmp.x, tmp.y + 1, tmp.z);
  26. else if (i == 5) return make_pos(tmp.x, tmp.y, tmp.z - 1);
  27. else if (i == 6) return make_pos(tmp.x, tmp.y, tmp.z + 1);
  28. }
  29.  
  30. bool skip(pos tmp) {
  31. if (tmp.x <= 0 || tmp.y <= 0 || tmp.z <= 0) return true;
  32. if (tmp.x > k || tmp.y > m || tmp.z > n) return true;
  33. if (arr[tmp.x][tmp.y][tmp.z] == -1) return true;
  34. if (map[tmp.x][tmp.y][tmp.z] >= 1) return true;
  35. return false;
  36. }
  37.  
  38. void process(pos tmp, pos xyz)
  39. {
  40. map[tmp.x][tmp.y][tmp.z] = map[xyz.x][xyz.y][xyz.z] + 1;
  41. q.push(tmp);
  42. }
  43.  
  44. void bfs()
  45. {
  46. while (!q.empty()) {
  47. pos xyz = q.front();
  48. q.pop();
  49. for (int i = 1; i <= 6; i++) {
  50. pos tmp = xyz;
  51. tmp = change(tmp, i);
  52. if (skip(tmp)) continue;
  53. process(tmp, xyz);
  54. }
  55. }
  56. int max1 = 0;
  57. for (int i = 1; i <= k; i++) {
  58. for (int j = 1; j <= m; j++) {
  59. for (int k1 = 1; k1 <= n; k1++) {
  60. if (map[i][j][k1] == 0 && arr[i][j][k1] != -1 && arr[i][j][k1] != 1) {
  61. cout << "-1\n";
  62. return;
  63. } else {
  64. max1 = max(max1, map[i][j][k1]);
  65. }
  66. }
  67. }
  68. }
  69. cout << max1 << "\n";
  70. }
  71.  
  72. int main()
  73. {
  74. ios::sync_with_stdio(false);
  75. cin.tie(NULL);
  76. cout.tie(NULL);
  77.  
  78. int cnt = 0;
  79. cin >> n >> m >> k;
  80. for (int i = 1; i <= k; i++) {
  81. for (int j = 1; j <= m; j++) {
  82. for (int k1 = 1; k1 <= n; k1++) {
  83. cin >> arr[i][j][k1];
  84. if (arr[i][j][k1] == 1) q.push(make_pos(i, j, k1));
  85. if (arr[i][j][k1] == 1 || arr[i][j][k1] == -1) cnt++;
  86. }
  87. }
  88. }
  89. if (cnt == n * m * k) cout << "0\n";
  90. else bfs();
  91. }
Success #stdin #stdout 0s 5760KB
stdin
3 2 1
1 1 0
-1 -1 -1
stdout
2