fork download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <queue>
  4. #include <utility>
  5. using namespace std;
  6. typedef pair<int, pair<int, int> > piii;
  7. int n,m,e;
  8. int arr[101][101][101] = {0,};
  9. int dx[6] = { 0,1,0,-1,0,0 };
  10. int dy[6] = { 1,0,-1,0,0,0 };
  11. int dz[6] = { 0,0,0,0,1,-1 };
  12. int count = 0;
  13. queue<piii> where;
  14.  
  15. bool findAll(){
  16. for(int h = 0; h < e; h++){
  17. for(int i = 0; i < m; i++){
  18. for(int j = 0; j < n; j++){
  19. if(arr[i][j][h] == 0){ return false;}
  20. if(count < arr[i][j][h]) count = arr[i][j][h];
  21. }
  22. }
  23. }return true;
  24. }
  25. bool print(){
  26. for(int h = 0; h < e; h++){
  27. for(int i = 0; i < m; i++){
  28. for(int j = 0; j < n; j++){
  29. printf("%d ",arr[i][j][h]);
  30. }puts("");
  31. }
  32. }return true;
  33. }
  34.  
  35. int main() {
  36. scanf("%d%d%d", &n, &m, &e);
  37. for(int h = 0; h < e; h++){
  38. for(int i = 0; i < m; i++){
  39. for(int j = 0; j < n; j++){
  40. scanf("%d",&arr[i][j][h]);
  41. if(arr[i][j][h] == 1){
  42. where.push({i,{j,h}});
  43. }
  44. }
  45. }
  46. }
  47. int kq = where.size();
  48. if(kq == n *m *e) count = 0;
  49. else if(kq == 0) count = -1;
  50. else{
  51.  
  52. while(!where.empty()){
  53. int y = where.front().first;
  54. int x = where.front().second.first;
  55. int z = where.front().second.second;
  56. where.pop();
  57. for(int i = 0; i < 6; i++){
  58. int n_y = y + dy[i];
  59. int n_x = x + dx[i];
  60. int n_z = z + dz[i];
  61. if( ((n_y >= 0 && n_y < m)
  62. && (n_z >= 0 && n_z < e)
  63. && (n_x >= 0 && n_x < n))
  64. && arr[n_y][n_x][n_z] == 0){
  65. arr[n_y][n_x][n_z] = arr[y][x][z] + 1;
  66. where.push({n_y,{n_x,n_z}});
  67. }
  68. }
  69. }
  70. if(findAll()) count--;
  71. else count = -1;
  72. }
  73.  
  74. printf("%d",count);
  75. return 0;
  76. }
Success #stdin #stdout 0s 19264KB
stdin
5 3 2
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
stdout
4