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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int n, m;
vector<string> in, tmp;
int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
int dy[] = {1, 0, -1, 1, -1, 1, 0, -1};
vp t, nt;
bool f(int a, int b) {
  int c = in[a][b] - '0';
  int co = 0;
  rep(i, 8) {
    int x = a + dx[i];
    int y = b + dy[i];
    if (in[x][y] == '.')
      co++;
  }
  if (co >= c) {
    tmp[a][b] = '.';
    return true;
  }
  return false;
}
bool F(bool e) {
  nt = vp(0);
  tmp = in;
  bool h = false;
  if (!e) {
    rep(i, n) rep(j, m) if (in[i][j] != '.' && f(i, j)) h = true,
                                                        nt.pb(pii(i, j));
  } else {
    rep(i, t.size()) rep(j, 8) {
      int x = t[i].first + dx[j];
      int y = t[i].second + dy[j];
      if (tmp[x][y] != '.' && f(x, y))
        h = true, nt.pb(pii(x, y));
    }
  }
  in = tmp;
  t = nt;
  return h;
}
int main() {
  cin >> n >> m;
  in = vector<string>(n);
  rep(i, n) cin >> in[i];
  int co = 0;
  if (F(0)) {
    co++;
    while (F(1))
      co++;
  }
  cout << co << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int n, m;
vector<string> in, tmp;
int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
int dy[] = {1, 0, -1, 1, -1, 1, 0, -1};
bool f(int a, int b) {
  int c = in[a][b] - '0';
  int co = 0;
  rep(i, 8) {
    int x = a + dx[i];
    int y = b + dy[i];
    if (in[x][y] == '.')
      co++;
  }
  if (co >= c) {
    tmp[a][b] = '.';
    return true;
  }
  return false;
}
bool F() {
  tmp = in;
  bool h = false;
  rep(i, n) rep(j, m) if (in[i][j] != '.' && f(i, j)) h = true;
  in = tmp;
  return h;
}
int main() {
  cin >> n >> m;
  in = vector<string>(n);
  rep(i, n) cin >> in[i];
  int co = 0;
  while (F())
    co++;
  cout << co << endl;
}