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

#include "iostream"
#include "math.h"
#include "queue"
#include "utility"

using namespace std;
int H, W;
char status[1002][1002];
int field[1002][1002];
pair<int, int> dir[8] = {make_pair(0, 1),  make_pair(1, 1),  make_pair(1, 0),
                         make_pair(1, -1), make_pair(0, -1), make_pair(-1, -1),
                         make_pair(-1, 0), make_pair(-1, 1)};
queue<pair<int, int>> Q;
queue<pair<int, int>> del;
bool flag[1002][1002];
int ans = 0;
int box = 0;
int num = 0;
int x, y;

int main() {
  cin >> H >> W;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> status[i][j];
      if (status[i][j] == '.') {
        field[i][j] = 0;
      } else {
        field[i][j] = status[i][j] - '0';
        flag[i][j] = true;
      }
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (field[i][j] != 0) {
        box = 0;
        for (int k = 0; k < 8; k++) {
          if (field[i + dir[k].first][j + dir[k].second] == 0)
            box++;
        }
        if (box >= field[i][j] && flag[i][j]) {
          Q.push(make_pair(i, j));
          flag[i][j] = false;
          ans = 1;
        }
      }
    }
  }
  del = Q;

  while (!del.empty()) {
    y = del.front().first;
    x = del.front().second;
    field[y][x] = 0;
    del.pop();
  }
  while (1) {
    num = Q.size();
    // cout << num <<" ";
    for (int i = 0; i < num; i++) {
      y = Q.front().first;
      x = Q.front().second;
      for (int j = 0; j < 8; j++) {
        if (field[y + dir[j].first][x + dir[j].second] != 0 &&
            y + dir[j].first >= 1 && y + dir[j].first <= H &&
            x + dir[j].second >= 1 && x + dir[j].second <= W) {
          box = 0;
          for (int k = 0; k < 8; k++) {
            if (field[y + dir[j].first + dir[k].first]
                     [x + dir[j].second + dir[k].second] == 0)
              box++;
          }
          if (box >= field[y + dir[j].first][x + dir[j].second] &&
              flag[y + dir[j].first][x + dir[j].second]) {
            Q.push(make_pair(y + dir[j].first, x + dir[j].second));
            flag[y + dir[j].first][x + dir[j].second] = false;
          }
        }
      }
      Q.pop();
    }
    // cout << Q.size() << "\\n";
    if (Q.empty())
      break;
    del = Q;
    ans++;
    // cout << del.size() << "\\n";
    while (!del.empty()) {
      y = del.front().first;
      x = del.front().second;
      field[y][x] = 0;
      del.pop();
    }
  }
  cout << ans << "\\n";
  return 0;
}

Error: MLE

Fixed submission:

#include "iostream"
#include "math.h"
#include "queue"
#include "utility"

using namespace std;
int H, W;
char status[1002][1002];
int field[1002][1002];
pair<int, int> dir[8] = {make_pair(0, 1),  make_pair(1, 1),  make_pair(1, 0),
                         make_pair(1, -1), make_pair(0, -1), make_pair(-1, -1),
                         make_pair(-1, 0), make_pair(-1, 1)};
queue<pair<int, int>> Q;
queue<pair<int, int>> del;
int ans = 0;
int box = 0;
int num = 0;
int x, y;

int main() {
  cin >> H >> W;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> status[i][j];
      if (status[i][j] == '.') {
        field[i][j] = 0;
      } else {
        field[i][j] = status[i][j] - '0';
      }
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (field[i][j] != 0) {
        box = 0;
        for (int k = 0; k < 8; k++) {
          if (field[i + dir[k].first][j + dir[k].second] == 0)
            box++;
        }
        if (box >= field[i][j]) {
          Q.push(make_pair(i, j));
          ans = 1;
        }
      }
    }
  }
  del = Q;

  while (!del.empty()) {
    y = del.front().first;
    x = del.front().second;
    field[y][x] = 0;
    del.pop();
  }
  while (1) {
    num = Q.size();
    // cout << num <<" ";
    for (int i = 0; i < num; i++) {
      y = Q.front().first;
      x = Q.front().second;
      for (int j = 0; j < 8; j++) {
        if (field[y + dir[j].first][x + dir[j].second] != 0 &&
            y + dir[j].first >= 1 && y + dir[j].first <= H &&
            x + dir[j].second >= 1 && x + dir[j].second <= W) {
          box = 0;
          for (int k = 0; k < 8; k++) {
            if (field[y + dir[j].first + dir[k].first]
                     [x + dir[j].second + dir[k].second] == 0)
              box++;
          }
          if (box >= field[y + dir[j].first][x + dir[j].second]) {
            Q.push(make_pair(y + dir[j].first, x + dir[j].second));
          }
        }
      }
      Q.pop();
    }
    // cout << Q.size() << "\\n";
    if (Q.empty())
      break;
    del = Q;
    ans++;
    // cout << del.size() << "\\n";
    while (!del.empty()) {
      y = del.front().first;
      x = del.front().second;
      field[y][x] = 0;
      del.pop();
    }
  }
  cout << ans << "\\n";
  return 0;
}