Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.0 kB
1
Indexable
Never
#include <stdio.h>
int main()
{
  int n, m, p, times;
  scanf("%d%d%d%d", &n, &m, &p, &times);
  int life[2][n][m][p];

  for(int i = 0;i < n;i++){
    for(int j = 0;j < m;j++){
      for(int l = 0;l < p;l++){
        int temp;
        scanf("%d", &temp);
        life[0][i][j][l] =temp;
      }
    }
  }

  for(int k = 0;k < times;k++){
    for(int i = 0; i < n;i++){
      for(int j = 0;j < m;j++){
        for(int l = 0;l < p;l++){
          int neighbor = 0;
          //checking neighbors
          for(int dx = -1;dx <= 1;dx++){
            for(int dy = -1;dy <= 1;dy++){
              for(int dz = -1;dz <= 1;dz++){
                if(!(dx == 0 && dy == 0 && dz == 0)){
                  int x = i + dx;
                  int y = j + dy;
                  int z = l + dz;
                  //calculate the numbers of nerghbors
                  if(x >= 0 && x < n && y >= 0 && y < m && z >= 0 && z < p && life[0][x][y][z] == 1){
                    neighbor++;
                  }
                }
              }
            }
          }
          //determine whether the cell is alive of dead
          if(life[0][i][j][l] == 1 && neighbor >= 4 && neighbor <= 8){
            life[1][i][j][l] = 1;
          }
          else if(life[0][i][j][l] = 0 && neighbor == 6){
            life[1][i][j][l] = 1;
          }
          else{
            life[1][i][j][l] = 0;
          }
        }
      }
    }

     //cover old map with new one, and take the new one to run again
    for(int i = 0;i < n;i++){
      for(int j = 0;j < m;j++){
        for(int l = 0;l < p;l++){
          life[0][i][j][l] = life[1][i][j][l];
        }
      }
    }
  }

  for(int i = 0;i < n;i++){
    for(int j = 0;j < m;j++){
      for(int l = 0;l < p;l++){
        if(l == p - 1){
          printf("%d\n", life[0][i][j][l]);
        }
        else{
          printf("%d ", life[0][i][j][l]);
        }
      }
    }
    printf("\n");
  }

 return 0;
//WTF is wrong with this code