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

#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int countaround(vector<vector<int>> &mp, int h, int w) {
  int cnt = 0;
  int dh[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
  int dw[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

  for (int i = 0; i < 8; ++i) {
    if (h + dh[i] >= 0 && h + dh[i] < (int)mp.size() && w + dw[i] >= 0 &&
        w + dw[i] < (int)mp[0].size() && mp[h + dh[i]][w + dw[i]] == 0)
      ++cnt;
  }
  return cnt;
}

bool checkcount(vector<vector<int>> &mp, int h, int w) {
  return countaround(mp, h, w) >= mp[h][w];
}

typedef pair<int, int> P;

int main() {
  int H, W;
  cin >> H >> W;

  vector<vector<int>> mp, updt;

  mp.resize(H);
  updt.resize(H);

  for (int i = 0; i < H; ++i) {
    string instr;
    cin >> instr;

    mp[i].resize(W);
    updt[i].resize(W);
    for (int j = 0; j < W; ++j) {
      if (instr[j] == '.')
        mp[i][j] = 0;
      else
        mp[i][j] = instr[j] - '0';
      updt[i][j] = 0;
    }
  }

  queue<P> q[2];
  int idxq = 0;

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (checkcount(mp, i, j)) {
        q[idxq].push(P(i, j));
        updt[i][j] = 1;
      }
    }
  }

  int dh[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
  int dw[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

  while (true) {
    while (!q[idxq % 2].empty()) {
      P p = q[idxq % 2].front();
      q[idxq % 2].pop();
      mp[p.first][p.second] = 0;
      for (int i = 0; i < 8; ++i) {
        if (p.first + dh[i] >= 0 && p.first + dh[i] < (int)mp.size() &&
            p.second + dw[i] >= 0 && p.second + dw[i] < (int)mp[0].size() &&
            updt[p.first + dh[i]][p.second + dw[i]] == 0 &&
            checkcount(mp, p.first + dh[i], p.second + dw[i])) {
          q[(idxq + 1) % 2].push(P(p.first + dh[i], p.second + dw[i]));
          updt[p.first + dh[i]][p.second + dw[i]] = 1;
        }
      }
    }

    ++idxq;
    if (q[idxq % 2].empty())
      break;
  }

  cout << idxq << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> P;

bool check_destroy(vector<vector<int>> &mp, int h, int w) {
  int cnt = 0;
  int dh[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
  int dw[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

  for (int i = 0; i < 8; ++i) {
    cnt += (mp[h + dh[i]][w + dw[i]] == 0 ? 1 : 0);
  }

  return mp[h][w] <= cnt;
}

bool check_stable(vector<vector<int>> &mp, int h, int w) {
  if (mp[h][w] == 9)
    return true;

  int cnt = 0;
  int dh[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
  int dw[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

  for (int i = 0; i < 8; ++i) {
    cnt += (mp[h + dh[i]][w + dw[i]] == 9 ? 1 : 0);
  }

  return mp[h][w] + cnt > 8;
}

int main() {
  int H, W;
  cin >> H >> W;

  queue<P> q[2];
  queue<P> dest[2];

  vector<vector<int>> mp(H);
  int ans = 0;

  for (int i = 0; i < H; ++i)
    mp[i].resize(W);

  for (int i = 0; i < H; ++i) {
    string instr;
    cin >> instr;
    for (int j = 0; j < W; ++j) {
      switch (instr[j]) {
      case '.':
        mp[i][j] = 0;
        break;
      default:
        mp[i][j] = instr[j] - '0';
        break;
      }
    }
  }

  for (int i = 1; i < H - 1; ++i) {
    for (int j = 1; j < W - 1; ++j) {
      if (!check_stable(mp, i, j))
        q[ans % 2].push(P(i, j));
    }
  }

  while (1) {
    int QCNT = q[ans % 2].size();

    while (!q[ans % 2].empty()) {
      P temp = q[ans % 2].front();
      q[ans % 2].pop();
      if (check_destroy(mp, temp.first, temp.second))
        dest[ans % 2].push(temp);
      else
        q[(ans + 1) % 2].push(temp);
    }

    while (!dest[ans % 2].empty()) {
      P temp = dest[ans % 2].front();
      dest[ans % 2].pop();
      mp[temp.first][temp.second] = 0;
    }

    if (QCNT == q[(ans + 1) % 2].size())
      break;
    ++ans;
  }

  cout << ans << endl;

  return 0;
}