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

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, s, n) for (int i = s; n > i; i++)
#define REP(i, n) rep(i, 0, n)
#define MODU 33
#define Range(x, a, b) ((a) <= (x) && (x) <= (b))
#define POWT(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
using namespace std;
typedef vector<int> Ivec;
typedef pair<int, int> pii;
typedef long long int ll;
const pii eight_Dir[8] = { //?????????
    {-1, 1}, {0, 1}, {1, 1}, {-1, 0}, {1, 0}, {-1, -1}, {0, -1}, {1, -1}};
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
int main() {
  int h, w;
  scanf("%d %d", &h, &w);
  vector<vector<int>> M(h, vector<int>(w)), dam(h, vector<int>(w));
  queue<pair<int, pii>> sara;
  REP(i, h) {
    scanf("%*c");
    REP(j, w) {
      scanf("%c", &M[i][j]);
      if (M[i][j] == '.') {
        sara.push({0, {i, j}});
        dam[i][j] = '.';
      } else
        M[i][j] -= '0';
    }
  }
  int ans = 0;
  while (sara.size()) {
    int turn = sara.front().first;
    pii pos = sara.front().second;
    sara.pop();
    REP(k, 8) {
      pii cur = {eight_Dir[k].first + pos.first,
                 eight_Dir[k].second + pos.second};
      if (Range(cur.first, 0, h - 1) && Range(cur.second, 0, w - 1)) {
        if (dam[cur.first][cur.second] != '.') {
          if ((++dam[cur.first][cur.second]) == M[cur.first][cur.second]) {
            dam[cur.first][cur.second] = '.';
            sara.push({turn + 1, cur});
            ans = turn + 1;
          }
        }
      }
    }
  }
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

struct Mapchip {
  unsigned char num : 4;
  unsigned char change : 1;
};

int main() {
  int w, h, s[2] = {}, e[2] = {};
  Mapchip map[1000][1000] = {};
  int dir[2][8] = {{-1, 0, 1, -1, 1, -1, 0, 1}, {-1, -1, -1, 0, 0, 1, 1, 1}};
  scanf("%d %d", &h, &w);
  for (int i = 0; h > i; i++) {
    scanf("%*c");
    for (int j = 0; w > j; j++) {
      char buf;
      scanf("%c", &buf);

      if (buf == '.')
        buf = '0';
      else {
        if (s[0] == 0) {
          s[0] = i;
          s[1] = j;
        }
        if (e[0] < i || e[1] < j) {
          e[0] = i;
          e[1] = j;
        }
      }

      map[i][j].num = buf - '0';
    }
  }

  int t = 1, count;
  for (count = 0; t; count++) {
    t = 0;
    for (int i = s[0]; e[0] >= i; i++) {
      for (int j = s[1]; e[1] >= j; j++) {
        if (map[i][j].num) {
          int emptyc = 0;
          for (int c = 0; 8 > c; c++) {
            if (!map[i + dir[0][c]][j + dir[1][c]].num &&
                !map[i + dir[0][c]][j + dir[1][c]].change)
              emptyc++;
          }
          if (emptyc >= map[i][j].num) {
            t = 1;
            map[i][j].num = 0;
            map[i][j].change = 1;
          }
        }
      }
    }
    for (int i = s[0]; e[0] >= i; i++) {
      for (int j = s[1]; e[1] >= j; j++) {
        map[i][j].change = 0;
      }
    }
  };
  printf("%d\\n", count - 1);
  return 0;
}