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

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

struct Data {
  int x, y;
  bool operator<(const Data &data) const {
    return (x == data.x) ? y < data.y : x < data.x;
  }
  bool operator==(const Data &data) const { return x == data.x && y == data.y; }
};
int h, w;
char c[1010][1010];

inline bool isValid(int nx, int ny) {
  return 0 <= nx && nx < w && 0 <= ny && ny < h;
}

int main() {
  cin >> h >> w;
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) c[i][j] = s[j];
  }
  int phase = 0;
  deque<Data> deq;
  rep(i, h) rep(j, w) if (c[i][j] != '.') {
    int cnt = 0;
    REP(dy, -1, 2) REP(dx, -1, 2) {
      int nx = j + dx, ny = i + dy;
      if (!isValid(nx, ny))
        continue;
      if (c[ny][nx] == '.')
        ++cnt;
    }
    if (cnt >= (c[i][j] - '0'))
      deq.push_back((Data){j, i});
  }

  while (!deq.empty()) {
    ++phase;
    deque<Data> next_deq;
    rep(i, deq.size()) c[deq[i].y][deq[i].x] = '.';
    rep(i, deq.size()) {
      REP(dy, -1, 2) REP(dx, -1, 2) {
        int nx = deq[i].x + dx, ny = deq[i].y + dy;
        if (!isValid(nx, ny))
          continue;
        if (c[ny][nx] == '.')
          continue;
        int cnt = 0;
        REP(dy2, -1, 2) REP(dx2, -1, 2) {
          int nx2 = nx + dx2, ny2 = ny + dy2;
          if (!isValid(nx2, ny2))
            continue;
          if (c[ny2][nx2] == '.')
            ++cnt;
        }
        if (cnt >= (c[ny][nx] - '0'))
          next_deq.push_back((Data){nx, ny});
      }
    }
    deq = next_deq;
    sort(deq.begin(), deq.end());
    deq.erase(unique(deq.begin(), deq.end()), deq.end());
  }

  cout << phase << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

struct Data {
  int x, y;
};
int h, w;
char c[1010][1010];

inline bool isValid(int nx, int ny) {
  return 0 <= nx && nx < w && 0 <= ny && ny < h;
}

int main() {
  cin >> h >> w;
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) c[i][j] = s[j];
  }
  int phase = 0;
  deque<Data> deq;
  rep(i, h) rep(j, w) if (c[i][j] != '.') {
    int cnt = 0;
    REP(dy, -1, 2) REP(dx, -1, 2) {
      int nx = j + dx, ny = i + dy;
      if (!isValid(nx, ny))
        continue;
      if (c[ny][nx] == '.')
        ++cnt;
    }
    if (cnt >= (c[i][j] - '0'))
      deq.push_back((Data){j, i});
  }

  while (!deq.empty()) {
    ++phase;
    deque<Data> next_deq;
    rep(i, deq.size()) c[deq[i].y][deq[i].x] = '.';
    rep(i, deq.size()) {
      REP(dy, -1, 2) REP(dx, -1, 2) {
        int nx = deq[i].x + dx, ny = deq[i].y + dy;
        if (!isValid(nx, ny))
          continue;
        if (c[ny][nx] == '.')
          continue;
        int cnt = 0;
        REP(dy2, -1, 2) REP(dx2, -1, 2) {
          int nx2 = nx + dx2, ny2 = ny + dy2;
          if (!isValid(nx2, ny2))
            continue;
          if (c[ny2][nx2] == '.')
            ++cnt;
        }
        if (cnt >= (c[ny][nx] - '0'))
          next_deq.push_back((Data){nx, ny});
      }
      swap(deq, next_deq);
    }
  }

  cout << phase << endl;
  return 0;
}