Problem p00535 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
#include <tuple>
using namespace std;
int x[1005][1005], H, W;
bool J[1005][1005];
int solve(int r, int s) {
  if (r <= 0 || s <= 0 || r > H || s > W)
    return 0;
  int B = 0;
  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      if (x[r + i][s + j] == 0)
        B++;
    }
  }
  return B;
}
int main() {
  cin >> H >> W;
  queue<tuple<int, int, int>> Q;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      char c;
      cin >> c;
      if (c == '.')
        x[i][j] = 0;
      else {
        x[i][j] = c - '0';
      }
    }
  }
  vector<pair<int, int>> vec1;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (solve(i, j) >= x[i][j] && x[i][j] != 0) {
        vec1.push_back(make_pair(i, j));
        J[i][j] = true;
        Q.push(make_tuple(i, j, 1));
      }
    }
  }
  if (vec1.size() == 0) {
    cout << "0" << endl;
    return 0;
  }
  int Life = 1;
  for (int i = 0; i < vec1.size(); i++) {
    x[vec1[i].first][vec1[i].second] = 0;
  }
  vec1.clear();
  while (!Q.empty()) {
    int a1 = get<0>(Q.front()), a2 = get<1>(Q.front()), a3 = get<2>(Q.front());
    Q.pop();
    if (a3 != Life) {
      for (int i = 0; i < vec1.size(); i++) {
        x[vec1[i].first][vec1[i].second] = 0;
      }
      vec1.clear();
      Life++;
    }
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (solve(a1 + i, a2 + j) >= x[a1 + i][a2 + j] &&
            x[a1 + i][a2 + j] != 0 && J[a1 + i][a2 + j] == false) {
          vec1.push_back(make_pair(a1 + i, a2 + j));
          J[a1 + i][a2 + j] = true;
          Q.push(make_tuple(a1 + i, a2 + j, a3 + 1));
        }
      }
    }
  }
  cout << Life << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <tuple>
using namespace std;
int x[1005][1005], H, W;
int solve(int r, int s) {
  if (r <= 0 || s <= 0 || r > H || s > W)
    return 0;
  int B = 0;
  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      if (x[r + i][s + j] == 0)
        B++;
    }
  }
  return B;
}
int main() {
  cin >> H >> W;
  queue<tuple<int, int, int>> Q;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      char c;
      cin >> c;
      if (c == '.')
        x[i][j] = 0;
      else {
        x[i][j] = c - '0';
      }
    }
  }
  vector<pair<int, int>> vec1;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (solve(i, j) >= x[i][j] && x[i][j] != 0) {
        vec1.push_back(make_pair(i, j));
        Q.push(make_tuple(i, j, 1));
      }
    }
  }
  if (vec1.size() == 0) {
    cout << "0" << endl;
    return 0;
  }
  int Life = 1;
  for (int i = 0; i < vec1.size(); i++) {
    x[vec1[i].first][vec1[i].second] = 0;
  }
  vec1.clear();
  while (!Q.empty()) {
    int a1 = get<0>(Q.front()), a2 = get<1>(Q.front()), a3 = get<2>(Q.front());
    Q.pop();
    if (a3 != Life) {
      for (int i = 0; i < vec1.size(); i++) {
        x[vec1[i].first][vec1[i].second] = 0;
      }
      vec1.clear();
      Life++;
    }
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (solve(a1 + i, a2 + j) >= x[a1 + i][a2 + j] &&
            x[a1 + i][a2 + j] != 0) {
          vec1.push_back(make_pair(a1 + i, a2 + j));
          Q.push(make_tuple(a1 + i, a2 + j, a3 + 1));
        }
      }
    }
  }
  cout << Life << endl;
  return 0;
}