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

#include <bits/stdc++.h>

#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
inline string numstr(int m) {
  str s = "";
  while (m > 0) {
    char c;
    int a = m / 10;
    if (a > 0)
      a = m % (a * 10);
    else
      a = m;
    c = (char)('0' + a);
    s += c;
    m /= 10;
  }
  str st = "";
  for (int i = s.size() - 1; i >= 0; i--) {
    st += s[i];
  }
  return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
int flag[1003][1003];
int num[1003][1003];
int dx[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int w, h;
int dy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
string c[1003];
bool in(int a, int b) {
  if (a >= 0 && b >= 0 && a < h && b < w)
    return true;
  else
    false;
}
signed main() {
  cin >> h >> w;
  rep(i, 0, h) { cin >> c[i]; }
  rep(i, 0, 1000) {
    rep(j, 0, 1000) {
      num[i][j] = 0;
      flag[i][j] = 0;
    }
  }
  rep(i, 0, h) {
    rep(j, 0, w) {
      if (c[i][j] == '.') {
        num[i][j] = 0;
        rep(k, 0, 8) {
          if ((i + dy[k]) >= 0 && (j + dx[k]) >= 0 && (i + dy[k]) < h &&
              (j + dx[k]) < w) {
            flag[i + dy[k]][j + dx[k]]++;
          }
        }
      } else {
        num[i][j] = (c[i][j] - '0');
      }
    }
  }
  int ans = 0;
  queue<pii> que;
  queue<pii> ser;
  rep(i, 0, h) {
    rep(j, 0, w) {
      if (num[i][j] == 0)
        continue;
      if (flag[i][j] >= num[i][j]) {
        num[i][j] = 0;
        que.push(mp(i, j));
      }
    }
  }
  while (1) {
    if (ans != 0) {
      if (ser.empty())
        break;
      while (!ser.empty()) {
        pii p = ser.front();
        ser.pop();
        int i = p.first;
        int j = p.second;
        if (num[i][j] == 0)
          continue;
        if (flag[i][j] >= num[i][j]) {
          num[i][j] = 0;
          que.push(mp(i, j));
          // flag[i][j]++;
        }
      }
    }
    if (que.empty())
      break;
    ans++;
    while (!que.empty()) {
      pii p = que.front();
      que.pop();
      int y = p.first;
      int x = p.second;
      rep(k, 0, 8) {
        if ((y + dy[k]) >= 0 && (x + dx[k]) >= 0 && (y + dy[k]) < h &&
            (x + dx[k]) < w) {
          if (num[y + dy[k]][x + dx[k]] != 0) {
            // if(flag[y+dy[k]][x+dx[k]]>=num[y+dy[k]][x+dx[k]]){
            // num[y+dy[k]][x+dx[k]]=0;
            ser.push(mp(y + dy[k], x + dx[k]));
            flag[y + dy[k]][x + dx[k]]++;
            //}
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
inline string numstr(int m) {
  str s = "";
  while (m > 0) {
    char c;
    int a = m / 10;
    if (a > 0)
      a = m % (a * 10);
    else
      a = m;
    c = (char)('0' + a);
    s += c;
    m /= 10;
  }
  str st = "";
  for (int i = s.size() - 1; i >= 0; i--) {
    st += s[i];
  }
  return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
int flag[1003][1003];
int num[1003][1003];
int dx[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int w, h;
int dy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
string c[1003];
bool in(int a, int b) {
  if (a >= 0 && b >= 0 && a < h && b < w)
    return true;
  else
    false;
}
signed main() {
  cin >> h >> w;
  rep(i, 0, h) { cin >> c[i]; }
  rep(i, 0, 1000) {
    rep(j, 0, 1000) {
      num[i][j] = 0;
      flag[i][j] = 0;
    }
  }
  rep(i, 0, h) {
    rep(j, 0, w) {
      if (c[i][j] == '.') {
        num[i][j] = 0;
        rep(k, 0, 8) {
          if ((i + dy[k]) >= 0 && (j + dx[k]) >= 0 && (i + dy[k]) < h &&
              (j + dx[k]) < w) {
            flag[i + dy[k]][j + dx[k]]++;
          }
        }
      } else {
        num[i][j] = (c[i][j] - '0');
      }
    }
  }
  int ans = 0;
  queue<pii> que;
  while (1) {
    rep(i, 0, h) {
      rep(j, 0, w) {
        if (num[i][j] == 0)
          continue;
        if (flag[i][j] >= num[i][j]) {
          num[i][j] = 0;
          que.push(mp(i, j));
        }
      }
    }
    if (que.empty())
      break;
    ans++;
    while (!que.empty()) {
      pii p = que.front();
      que.pop();
      int y = p.first;
      int x = p.second;
      rep(k, 0, 8) {
        if ((y + dy[k]) >= 0 && (x + dx[k]) >= 0 && (y + dy[k]) < h &&
            (x + dx[k]) < w)
          flag[y + dy[k]][x + dx[k]]++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}