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

#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int h, w;
char bch[1003][1003];

int bfs() {
  int ans = 0, cas[1003][1003], c;
  vector<pair<int, int>> que, vec;
  for (int i = 1; i < h - 1; i++) {
    for (int j = 1; j < w - 1; j++) {
      if (bch[i][j] != '.') {
        c = 0;
        if (bch[i - 1][j - 1] == '.')
          c++;
        if (bch[i - 1][j] == '.')
          c++;
        if (bch[i - 1][j + 1] == '.')
          c++;
        if (bch[i][j - 1] == '.')
          c++;
        if (bch[i][j + 1] == '.')
          c++;
        if (bch[i + 1][j - 1] == '.')
          c++;
        if (bch[i + 1][j] == '.')
          c++;
        if (bch[i + 1][j + 1] == '.')
          c++;
        cas[i][j] = bch[i][j] - '0' - c;
        if (cas[i][j] <= 0)
          que.push_back({i, j});
      }
    }
  }
  while (1) {
    if (que.empty()) {
      ans++;
      for (auto u : vec) {
        int i = u.first, j = u.second;
        if (bch[i - 1][j - 1] != '.') {
          cas[i - 1][j - 1]--;
          if (cas[i - 1][j - 1] == 0)
            que.push_back({i - 1, j - 1});
        }
        if (bch[i - 1][j] != '.') {
          cas[i - 1][j]--;
          if (cas[i - 1][j] == 0)
            que.push_back({i - 1, j});
        }
        if (bch[i - 1][j + 1] != '.') {
          cas[i - 1][j + 1]--;
          if (cas[i - 1][j + 1] == 0)
            que.push_back({i - 1, j + 1});
        }
        if (bch[i][j - 1] != '.') {
          cas[i][j - 1]--;
          if (cas[i][j - 1] == 0)
            que.push_back({i, j - 1});
        }
        if (bch[i][j + 1] != '.') {
          cas[i][j + 1]--;
          if (cas[i][j + 1] == 0)
            que.push_back({i, j + 1});
        }
        if (bch[i + 1][j - 1] != '.') {
          cas[i + 1][j - 1]--;
          if (cas[i + 1][j - 1] == 0)
            que.push_back({i + 1, j - 1});
        }
        if (bch[i + 1][j] != '.') {
          cas[i + 1][j]--;
          if (cas[i + 1][j] == 0)
            que.push_back({i + 1, j});
        }
        if (bch[i + 1][j + 1] != '.') {
          cas[i + 1][j + 1]--;
          if (cas[i + 1][j + 1] == 0)
            que.push_back({i + 1, j + 1});
        }
      }
      vec.clear();
    }
    if (que.empty())
      break;
    vec.swap(que);
  }
  return ans;
}

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> bch[i][j];
  cout << bfs() << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
#define INV -10

using namespace std;

int h, w;
char bch[1003][1003];

int wav(int i, int j) {
  int c = 0;
  if (bch[i - 1][j - 1] == '.')
    c++;
  if (bch[i - 1][j] == '.')
    c++;
  if (bch[i - 1][j + 1] == '.')
    c++;
  if (bch[i][j - 1] == '.')
    c++;
  if (bch[i][j + 1] == '.')
    c++;
  if (bch[i + 1][j - 1] == '.')
    c++;
  if (bch[i + 1][j] == '.')
    c++;
  if (bch[i + 1][j + 1] == '.')
    c++;
  return c;
}

int bfs() {
  int ans = 0, cas[1003][1003];
  bool done[1003][1003];
  vector<pair<int, int>> que, vec;
  memset(cas, -10, sizeof(cas));
  for (int i = 1; i < h - 1; i++) {
    for (int j = 1; j < w - 1; j++) {
      if (bch[i][j] != '.') {
        cas[i][j] = (bch[i][j] - '0') - wav(i, j);
        if (cas[i][j] <= 0)
          que.push_back({i, j});
      }
    }
  }
  while (1) {
    if (que.empty()) {
      ans++;
      for (auto u : vec) {
        int i = u.first, j = u.second;
        cas[i][j] = INV;
      }
      memset(done, false, sizeof(done));
      for (auto u : vec) {
        int i = u.first, j = u.second;
        if (cas[i - 1][j - 1] != INV && !done[i - 1][j - 1]) {
          cas[i - 1][j - 1]--;
          if (cas[i - 1][j - 1] == 0) {
            done[i - 1][j - 1] = true;
            que.push_back({i - 1, j - 1});
          }
        }
        if (cas[i - 1][j] != INV && !done[i - 1][j]) {
          cas[i - 1][j]--;
          if (cas[i - 1][j] == 0) {
            done[i - 1][j] = true;
            que.push_back({i - 1, j});
          }
        }
        if (cas[i - 1][j + 1] != INV && !done[i - 1][j + 1]) {
          cas[i - 1][j + 1]--;
          if (cas[i - 1][j + 1] == 0) {
            done[i - 1][j + 1] = true;
            que.push_back({i - 1, j + 1});
          }
        }
        if (cas[i][j - 1] != INV && !done[i][j - 1]) {
          cas[i][j - 1]--;
          if (cas[i][j - 1] == 0) {
            done[i][j - 1] = true;
            que.push_back({i, j - 1});
          }
        }
        if (cas[i][j + 1] != INV && !done[i][j + 1]) {
          cas[i][j + 1]--;
          if (cas[i][j + 1] == 0) {
            done[i][j + 1] = true;
            que.push_back({i, j + 1});
          }
        }
        if (cas[i + 1][j - 1] != INV && !done[i + 1][j - 1]) {
          cas[i + 1][j - 1]--;
          if (cas[i + 1][j - 1] == 0) {
            done[i + 1][j - 1] = true;
            que.push_back({i + 1, j - 1});
          }
        }
        if (cas[i + 1][j] != INV && !done[i + 1][j]) {
          cas[i + 1][j]--;
          if (cas[i + 1][j] == 0) {
            done[i + 1][j] = true;
            que.push_back({i + 1, j});
          }
        }
        if (cas[i + 1][j + 1] != INV && !done[i + 1][j + 1]) {
          cas[i + 1][j + 1]--;
          if (cas[i + 1][j + 1] == 0) {
            done[i + 1][j + 1] = true;
            que.push_back({i + 1, j + 1});
          }
        }
      }
      vec.clear();
    }
    if (que.empty())
      break;
    vec.swap(que);
  }
  return ans;
}

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> bch[i][j];
  cout << bfs() << '\\n';
  return 0;
}