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

#include <bits/stdc++.h>
using namespace std;

#define Rep(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) Rep(i, 0, n)

int movX[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int movY[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
typedef pair<int, int> pii;

template <class T> T read() {
  T t;
  cin >> t;
  return t;
}

int main() {
  int h = read<int>(), w = read<int>();
  int mp[h][w];
  rep(i, h) rep(j, w) {
    char c = read<char>();
    if (c == '.') {
      mp[i][j] = 0;
    } else {
      mp[i][j] = c - '0';
    }
  }
  int around[h][w];
  Rep(i, 1, h - 1) {
    Rep(j, 1, w - 1) {
      int a = 0;
      rep(k, 8) {
        int x = i + movX[k];
        int y = j + movY[k];
        if (!mp[x][y]) {
          a++;
        }
      }
      around[i][j] = a;
    }
  }

  vector<pii> list;
  Rep(i, 1, h - 1) {
    Rep(j, 1, w - 1) {
      if (mp[i][j] > 0 && around[i][j] >= mp[i][j]) {
        list.push_back(pii(i, j));
      }
    }
  }
  if (list.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  for (vector<pii>::iterator it = list.begin(); it != list.end(); it++) {
    int i = it->first, j = it->second;
    mp[i][j] = 0;
    rep(k, 8) {
      int x = i + movX[k];
      int y = j + movY[k];
      around[x][y]++;
    }
    // printf("(%d, %d)\\n",i,j);
  }
  // printf("=============\\n");

  int count = 1;
  while (true) {
    vector<pii> list2;
    for (vector<pii>::iterator it = list.begin(); it != list.end(); it++) {
      int i = it->first, j = it->second;
      // cout << i << " " << j << endl;
      rep(k, 8) {
        int x = i + movX[k];
        int y = j + movY[k];
        if (mp[x][y] > 0 && around[x][y] >= mp[x][y]) {
          list2.push_back(pii(x, y));
          // cout << "    " <<  i << " " << j << endl;
        }
      }
    }
    if (list2.size() == 0) {
      cout << count << endl;
      return 0;
    }
    //		cout << list2.size() << endl;
    count++;
    vector<pii> list3;
    for (vector<pii>::iterator it = list2.begin(); it != list2.end(); it++) {
      int i = it->first, j = it->second;
      if (mp[i][j]) {
        list3.push_back(*it);
        mp[i][j] = 0;
        rep(k, 8) {
          int x = i + movX[k];
          int y = j + movY[k];
          around[x][y]++;
        }
        // printf("(%d, %d)\\n",i,j);
      }
    }
    // printf("=============\\n");
    std::vector<pii>().swap(list);
    std::vector<pii>().swap(list2);
    list = list3;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define Rep(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) Rep(i, 0, n)

int movX[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int movY[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
typedef pair<int, int> pii;

template <class T> T read() {
  T t;
  cin >> t;
  return t;
}

int main() {
  int h = read<int>(), w = read<int>();
  int mp[h][w];
  rep(i, h) rep(j, w) {
    char c = read<char>();
    if (c == '.') {
      mp[i][j] = 0;
    } else {
      mp[i][j] = c - '0';
    }
  }
  int around[h][w];
  Rep(i, 1, h - 1) {
    Rep(j, 1, w - 1) {
      int a = 0;
      rep(k, 8) {
        int x = i + movX[k];
        int y = j + movY[k];
        if (!mp[x][y]) {
          a++;
        }
      }
      around[i][j] = a;
    }
  }

  vector<pii> list;
  Rep(i, 1, h - 1) {
    Rep(j, 1, w - 1) {
      if (mp[i][j] > 0 && around[i][j] >= mp[i][j]) {
        list.push_back(pii(i, j));
      }
    }
  }
  if (list.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  for (vector<pii>::iterator it = list.begin(); it != list.end(); it++) {
    int i = it->first, j = it->second;
    mp[i][j] = 0;
    rep(k, 8) {
      int x = i + movX[k];
      int y = j + movY[k];
      around[x][y]++;
    }
    // printf("(%d, %d)\\n",i,j);
  }
  // printf("=============\\n");

  int count = 1;
  while (true) {
    vector<pii> list2;
    for (vector<pii>::iterator it = list.begin(); it != list.end(); it++) {
      int i = it->first, j = it->second;
      // cout << i << " " << j << endl;
      rep(k, 8) {
        int x = i + movX[k];
        int y = j + movY[k];
        if (mp[x][y] > 0 && around[x][y] >= mp[x][y]) {
          list2.push_back(pii(x, y));
          // cout << "    " <<  i << " " << j << endl;
        }
      }
    }
    if (list2.size() == 0) {
      cout << count << endl;
      return 0;
    }
    count++;
    for (vector<pii>::iterator it = list2.begin(); it != list2.end(); it++) {
      int i = it->first, j = it->second;
      if (mp[i][j]) {
        mp[i][j] = 0;
        rep(k, 8) {
          int x = i + movX[k];
          int y = j + movY[k];
          around[x][y]++;
        }
        // printf("(%d, %d)\\n",i,j);
      }
    }
    // printf("=============\\n");
    std::vector<pii>().swap(list);
    list = list2;
  }
}