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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef pair<int, int> P;

int main() {

  int H, W;

  cin >> H >> W;

  int ans = 0;

  string map[1001];

  for (int i = 0; i < H; i++) {
    cin >> map[i];
  }

  queue<P> que;

  for (int i = 1; i < H - 1; i++) {
    for (int j = 1; j < W - 1; j++) {
      if (map[i][j] == '.') {
        continue;
      }
      int num = map[i][j] - '0';
      int count = 0;
      for (int x = -1; x <= 1; x++) {
        for (int y = -1; y <= 1; y++) {
          if (x == 0 && y == 0) {
            continue;
          }
          if (map[i + y][j + x] == '.') {
            count++;
          }
        }
      }
      if (count >= num) {
        P p;
        p.first = i;
        p.second = j;
        que.push(p);
      }
    }
  }

  int q_size = que.size();

  for (int i = 0; i < q_size; i++) {
    P p = que.front();
    que.pop();
    map[p.first][p.second] = '.';
    que.push(p);
  }
  /*
  for(int i = 0; i < H; i++){
          cout << map[i] << endl;
  }
  */
  while (!que.empty()) {
    ans++;
    queue<P> que_next;
    while (!que.empty()) {
      P p = que.front();
      que.pop();

      for (int i = p.first - 1; i <= p.first + 1; i++) {
        for (int j = p.second - 1; j <= p.second + 1; j++) {
          if (map[i][j] == '.') {
            continue;
          }
          int num = map[i][j] - '0';
          int count = 0;
          for (int x = -1; x <= 1; x++) {
            for (int y = -1; y <= 1; y++) {
              if (x == 0 && y == 0) {
                continue;
              }
              if (map[i + y][j + x] == '.') {
                count++;
              }
            }
          }

          if (count >= num) {
            P p2;
            p2.first = i;
            p2.second = j;
            que_next.push(p2);
          }
        }
      }
    }

    que = que_next;

    int q_size = que.size();

    for (int i = 0; i < q_size; i++) {
      P p = que.front();
      que.pop();
      if (map[p.first][p.second] != '.') {
        map[p.first][p.second] = '.';
        que.push(p);
      }
    }
    /*
    for(int i = 0; i < H; i++){
            cout << map[i] << endl;
    }
    */
  }

  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef pair<int, int> P;

int main() {

  int H, W;

  cin >> H >> W;

  int ans = 0;

  string map[1001];

  for (int i = 0; i < H; i++) {
    cin >> map[i];
  }

  queue<P> que, que_empty;

  for (int i = 1; i < H - 1; i++) {
    for (int j = 1; j < W - 1; j++) {
      if (map[i][j] == '.') {
        continue;
      }
      int num = map[i][j] - '0';
      int count = 0;
      for (int x = -1; x <= 1; x++) {
        for (int y = -1; y <= 1; y++) {
          if (x == 0 && y == 0) {
            continue;
          }
          if (map[i + y][j + x] == '.') {
            count++;
          }
        }
      }
      if (count >= num) {
        P p;
        p.first = i;
        p.second = j;
        que.push(p);
      }
    }
  }

  for (int i = 0; i < que.size(); i++) {
    P p = que.front();
    que.pop();
    map[p.first][p.second] = '.';
    que.push(p);
  }
  /*
  for(int i = 0; i < H; i++){
          cout << map[i] << endl;
  }
  */
  while (!que.empty()) {
    ans++;
    queue<P> que_next = que_empty;
    while (!que.empty()) {
      P p = que.front();
      que.pop();

      for (int i = p.first - 1; i <= p.first + 1; i++) {
        for (int j = p.second - 1; j <= p.second + 1; j++) {
          if (map[i][j] == '.') {
            continue;
          }
          int num = map[i][j] - '0';
          int count = 0;
          for (int x = -1; x <= 1; x++) {
            for (int y = -1; y <= 1; y++) {
              if (x == 0 && y == 0) {
                continue;
              }
              if (map[i + y][j + x] == '.') {
                count++;
              }
            }
          }
          if (count >= num) {
            P p;
            p.first = i;
            p.second = j;
            que_next.push(p);
          }
        }
      }
    }

    que = que_next;

    for (int i = 0; i < que.size(); i++) {
      P p = que.front();
      que.pop();
      if (map[p.first][p.second] != '.') {
        map[p.first][p.second] = '.';
      }
      que.push(p);
    }
    /*
    for(int i = 0; i < H; i++){
            cout << map[i] << endl;
    }
    */
  }

  cout << ans << endl;

  return 0;
}