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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 28;
const ll INF = 1ll << 50;
const double pi = acos(-1);
const double eps = 1e-8;
const int mod = 1e9 + 7;
const vi emp;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int DX[8] = {-1, -1, -1, 0, 0, 1, 1, 1},
          DY[8] = {1, 0, -1, 1, -1, 1, 0, -1};

int h, w;
vvi a;

int main() {
  cin >> h >> w;
  a = vvi(h, vi(w));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      char t;
      cin >> t;
      if (t != '.')
        a[i][j] = t - '0';
    }
  queue<pip> q;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (!a[i][j])
        q.push(pip(1, P(i, j)));
  int ans = 0;
  while (!q.empty()) {
    pip p = q.front();
    q.pop();
    int x = p.second.first, y = p.second.second;
    for (int i = 0; i < 8; i++) {
      int cx = x + DX[i], cy = y + DY[i];
      if (cx >= 0 && cx < h && cy >= 0 && cy < w) {
        a[cx][cy]--;
        if (!a[cx][cy]) {
          ans = p.first;
          q.push(pip(ans + 1, P(cx, cy)));
        }
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 28;
const ll INF = 1ll << 50;
const double pi = acos(-1);
const double eps = 1e-8;
const int mod = 1e9 + 7;
const vi emp;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int DX[8] = {-1, -1, -1, 0, 0, 1, 1, 1},
          DY[8] = {1, 0, -1, 1, -1, 1, 0, -1};

int h, w;
vvi a;

bool func() {
  vvi b = a;
  bool flag = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (b[i][j]) {
        int num = 0;
        for (int k = 0; k < 8; k++) {
          int cx = i + DX[k], cy = j + DY[k];
          if (cx >= 0 && cx < h && cy >= 0 && cy < w && !b[cx][cy])
            num++;
        }
        if (a[i][j] <= num) {
          flag = 1;
          a[i][j] = 0;
        }
      }
  return flag;
}

int main() {
  cin >> h >> w;
  a = vvi(h, vi(w));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      char t;
      cin >> t;
      if (t != '.')
        a[i][j] = t - '0';
    }
  int ans = 0;
  while (func())
    ans++;
  cout << ans << endl;
}