Problem p01000 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int lifegame[110][5][5][5]; // (d, x, y, z)

bool inside(int x, int y, int z) {
  return 0 <= x && x < 5 && 0 <= y && y < 5 && 0 <= z && z < 5;
}

int main() {
  string line;
  int N, no = 1;
  while (true) {
    cin >> N;
    if (N == 0)
      break;

    for (int z = 0; z < 5; z++) {
      for (int x = 0; x < 5; x++) {
        cin >> line;
        for (int y = 0; y < 5; y++) {
          lifegame[0][x][y][z] = line[y] - '0';
        }
      }
    }

    int M1, M2;
    cin >> M1;
    vector<int> a(M1);
    for (int i = 0; i < M1; i++) {
      cin >> a[i];
    }

    cin >> M2;
    vector<int> b(M2);
    for (int i = 0; i < M2; i++) {
      cin >> b[i];
    }
    for (int d = 0; d < N; d++) {
      for (int x = 0; x < 5; x++) {
        for (int y = 0; y < 5; y++) {
          for (int z = 0; z < 5; z++) {
            int num = 0;
            for (int dx = -1; dx <= 1; dx++) {
              for (int dy = -1; dy <= 1; dy++) {
                for (int dz = -1; dz <= 1; dz++) {
                  int nx = x + dx, ny = y + dy, nz = z + dz;
                  if (!inside(nx, ny, nz) || (nx == x && ny == y && nz == z))
                    continue;
                  num += lifegame[d][nx][ny][nz];
                }
              }
            }
            bool live = false, die = true;
            for (int i = 0; i < M1; i++) {
              if (a[i] == num)
                live = true;
            }
            for (int i = 0; i < M2; i++) {
              if (b[i] == num)
                die = false;
            }

            lifegame[d + 1][x][y][z] = lifegame[d][x][y][z];
            if (lifegame[d][x][y][z] > 0 && die) {

              lifegame[d + 1][x][y][z] = 0;
            } else if (lifegame[d][x][y][z] == 0 && live) {
              lifegame[d + 1][x][y][z] = 1;
            }
          }
        }
      }
    }

    if (no > 1)
      cout << endl;
    cout << "Case " << no++ << ":" << endl;
    for (int z = 0; z < 5; z++) {
      if (z > 0)
        cout << endl;
      for (int x = 0; x < 5; x++) {
        for (int y = 0; y < 5; y++) {
          cout << lifegame[N][x][y][z];
        }
        cout << endl;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int lifegame[110][5][5][5]; // (d, x, y, z)

bool inside(int x, int y, int z) {
  return 0 <= x && x < 5 && 0 <= y && y < 5 && 0 <= z && z < 5;
}

int main() {
  string line;
  int N, no = 1;
  while (true) {
    /*
    getline(cin, line);
    N = stoi(line);
    */
    cin >> N;
    cin.ignore();
    if (N == 0)
      break;

    for (int z = 0; z < 5; z++) {
      for (int x = 0; x < 5; x++) {
        cin >> line;
        // getline(cin, line);
        for (int y = 0; y < 5; y++) {
          lifegame[0][x][y][z] = line[y] - '0';
        }
      }
      cin.ignore();
      getline(cin, line);
    }

    int M1, M2;
    // getline(cin, line);
    cin >> M1;
    vector<int> a(M1);
    // cin.ignore();
    for (int i = 0; i < M1; i++) {
      cin >> a[i];
    }
    // cin.ignore();

    cin >> M2;
    vector<int> b(M2);
    for (int i = 0; i < M2; i++) {
      cin >> b[i];
    }
    cin.ignore();
    // simulate
    for (int d = 0; d < N; d++) {
      for (int x = 0; x < 5; x++) {
        for (int y = 0; y < 5; y++) {
          for (int z = 0; z < 5; z++) {
            int num = 0;
            for (int dx = -1; dx <= 1; dx++) {
              for (int dy = -1; dy <= 1; dy++) {
                for (int dz = -1; dz <= 1; dz++) {
                  int nx = x + dx, ny = y + dy, nz = z + dz;
                  if (!inside(nx, ny, nz) || (nx == x && ny == y && nz == z))
                    continue;
                  num += lifegame[d][nx][ny][nz];
                }
              }
            }
            bool live = false, die = true;
            for (int i = 0; i < M1; i++) {
              if (a[i] == num)
                live = true;
            }
            for (int i = 0; i < M2; i++) {
              if (b[i] == num)
                die = false;
            }

            lifegame[d + 1][x][y][z] = lifegame[d][x][y][z];
            if (lifegame[d][x][y][z] > 0 && die) {

              lifegame[d + 1][x][y][z] = 0;
            } else if (lifegame[d][x][y][z] == 0 && live) {
              lifegame[d + 1][x][y][z] = 1;
            }
          }
        }
      }
    }

    if (no > 1)
      cout << endl << endl;
    cout << "Case " << no++ << ":" << endl;
    for (int z = 0; z < 5; z++) {
      if (z > 0)
        cout << endl << endl;
      for (int x = 0; x < 5; x++) {
        for (int y = 0; y < 5; y++) {
          cout << lifegame[N][x][y][z];
        }
        if (x < 4)
          cout << endl;
      }
    }
  }

  return 0;
}