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

#include <iostream>
#include <queue>
#include <string>
#include <utility>
using namespace std;
typedef pair<int, int> P;

string sand[1005];
int sara[1005][1005];
queue<P> now;
queue<P> nxt;

int main() {
  int h, w;
  cin >> h >> w;

  for (int i = 0; i < h; i++)
    cin >> sand[i];

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (sand[i][j] != '.') {
        for (int k = -1; k <= 1; k++) {
          for (int l = -1; l <= 1; l++) {
            if (sand[i + k][j + l] == '.')
              sara[i][j]++;
          }
        }
      }
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (sand[i][j] != '.') {
        int a = sand[i][j] - '0';
        if (a <= sara[i][j]) {
          now.push(make_pair(i, j));
        }
      }
    }
  }

  int ans = 0;
  while (now.size()) {
    ans++;
    int a = now.size();
    queue<P> q;

    for (int i = 0; i < a; i++) {
      P p = now.front();
      now.pop();

      int f = p.first, s = p.second;

      if (sand[f][s] != '.' && (sara[f][s] >= sand[f][s] - '0')) {
        sand[f][s] = '.';

        for (int j = -1; j <= 1; j++) {
          for (int k = -1; k <= 1; k++) {
            if (sand[f + j][s + k] != '.') {
              q.push(make_pair(f + j, s + k));
            }
            sara[f + j][s + k]++;
          }
        }
      }
    }

    int b = q.size();
    for (int i = 0; i < b; i++) {
      P pa = q.front();
      q.pop();
      if ((sand[pa.first][pa.second] != '.') &&
          sara[pa.first][pa.second] >= (sand[pa.first][pa.second] - '0'))
        nxt.push(pa);
    }

    queue<P> kari;
    now = nxt;
    nxt = kari;
  }

  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <utility>
using namespace std;
typedef pair<int, int> P;

string sand[1005];
int sara[1005][1005];
queue<P> now;
queue<P> nxt;

int main() {
  int h, w;
  cin >> h >> w;

  for (int i = 0; i < h; i++)
    cin >> sand[i];

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (sand[i][j] != '.') {
        for (int k = -1; k <= 1; k++) {
          for (int l = -1; l <= 1; l++) {
            if (sand[i + k][j + l] == '.')
              sara[i][j]++;
          }
        }
      }
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (sand[i][j] != '.') {
        int a = sand[i][j] - '0';
        if (a <= sara[i][j]) {
          now.push(make_pair(i, j));
        }
      }
    }
  }

  int ans = 0;

  while (now.size()) {
    int a = now.size();
    queue<P> q;

    for (int i = 0; i < a; i++) {
      P p = now.front();
      now.pop();

      int f = p.first, s = p.second;

      sand[f][s] = '.';
      sara[f][s] = 0;

      for (int j = -1; j <= 1; j++) {
        for (int k = -1; k <= 1; k++) {
          if (sand[f + j][s + k] != '.')
            q.push(make_pair(f + j, s + k));
        }
      }
    }

    int b = q.size();
    for (int i = 0; i < b; i++) {
      P pa = q.front();
      q.pop();
      int m = 0;
      for (int j = -1; j <= 1; j++) {
        for (int k = -1; k <= 1; k++) {
          if (sand[pa.first + j][pa.second + k] == '.')
            m++;
        }
      }
      sara[pa.first][pa.second] = m;
      if (sara[pa.first][pa.second] >= sand[pa.first][pa.second] - '0')
        nxt.push(pa);
    }

    queue<P> kari;
    now = nxt;
    nxt = kari;

    ans++;
  }

  cout << ans << endl;
  return 0;
}