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

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define FOR(I, F, N) for (int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF (1 << 28)

template <typename T> void remove(vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dy[8] = {1, 1, 1, 0, -1, -1, -1, 0};

int w, h, ans = 0;
int state[1001][1001];
int score[1001][1001];
void show() {
  rep(i, h) {
    rep(j, w) {
      if (state[j][i] == 0)
        cout << '.';
      else
        cout << state[j][i];
    }
    cout << endl;
  }
}

int main(void) {
  cin >> h >> w;
  fill_n(*state, 1001 * 1001, -1);
  rep(i, h) rep(j, w) {
    char c;
    cin >> c;
    if (c == '.')
      state[j][i] = 0;
    else
      state[j][i] = c - '0';
  }
  queue<P> que;
  rep(i, h) rep(j, w) {
    if (state[j][i] == 0)
      continue;
    int count = 0;
    rep(k, 8) {
      int nx = j + dx[k];
      int ny = i + dy[k];
      if (nx < 0 || ny < 0 || state[nx][ny] == -1)
        continue;
      if (state[nx][ny] == 0)
        count++;
    }

    score[j][i] = count;
    if (state[j][i] <= count) {
      que.push(P(j, i));
    }
  }
  /// show();
  int memo[1001][1001] = {0};
  while (!que.empty()) {
    queue<P> reQue;
    while (!que.empty()) {
      reQue.push(que.front());
      /// cout << "(" << que.front().first << "," << que.front().second << ")"
      /// << endl;
      P p = que.front();
      int x = p.first;
      int y = p.second;
      state[x][y] = 0;
      score[x][y] = 0;
      rep(k, 8) {
        int nx = x + dx[k];
        int ny = y + dy[k];
        score[nx][ny]++;
      }
      que.pop();
    }
    while (!reQue.empty()) {
      P p = reQue.front();
      reQue.pop();
      int x = p.first;
      int y = p.second;
      /// cout << "(" << x << "," << y << ")" << endl;
      rep(k, 8) {
        int nx = x + dx[k];
        int ny = y + dy[k];
        if (nx < 0 || ny < 0 || state[nx][ny] == -1)
          continue;
        if (state[nx][ny] == 0 || state[nx][ny] == 9)
          continue;
        if (state[nx][ny] <= score[nx][ny] && !memo[nx][ny]) {
          memo[nx][ny] = 1;
          que.push(P(nx, ny));
        }
      }
    }
    ans++;
  }
  cout << ans << endl;

  return 0;
}

int StrToInt(string s) {
  stringstream ss;
  ss << s;
  int val;
  ss >> val;
  return val;
}
string IntToStr(int i) {
  stringstream ss;
  ss << i;
  return ss.str();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define FOR(I, F, N) for (int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF (1 << 28)

template <typename T> void remove(vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dy[8] = {1, 1, 1, 0, -1, -1, -1, 0};

int w, h, ans = 0;
int state[1001][1001];
int score[1001][1001];
void show() {
  rep(i, h) {
    rep(j, w) {
      if (state[j][i] == 0)
        cout << '.';
      else
        cout << state[j][i];
    }
    cout << endl;
  }
}

int main(void) {
  cin >> h >> w;
  fill_n(*state, 1001 * 1001, -1);
  rep(i, h) rep(j, w) {
    char c;
    cin >> c;
    if (c == '.')
      state[j][i] = 0;
    else
      state[j][i] = c - '0';
  }
  queue<P> que;
  int memo[1001][1001] = {0};
  rep(i, h) rep(j, w) {
    if (state[j][i] == 0)
      continue;
    int count = 0;
    rep(k, 8) {
      int nx = j + dx[k];
      int ny = i + dy[k];
      if (nx < 0 || ny < 0 || state[nx][ny] == -1)
        continue;
      if (state[nx][ny] == 0)
        count++;
    }

    score[j][i] = count;
    if (state[j][i] <= count && !memo[j][i]) {
      memo[j][i] = true;
      que.push(P(j, i));
    }
  }
  /// show();
  while (!que.empty()) {
    queue<P> reQue;
    while (!que.empty()) {
      reQue.push(que.front());
      /// cout << "(" << que.front().first << "," << que.front().second << ")"
      /// << endl;
      P p = que.front();
      int x = p.first;
      int y = p.second;
      state[x][y] = 0;
      score[x][y] = 0;
      rep(k, 8) {
        int nx = x + dx[k];
        int ny = y + dy[k];
        score[nx][ny]++;
      }
      que.pop();
    }
    int memo2[1001][1001] = {0};
    while (!reQue.empty()) {
      P p = reQue.front();
      reQue.pop();
      int x = p.first;
      int y = p.second;
      /// cout << "(" << x << "," << y << ")" << endl;
      rep(k, 8) {
        int nx = x + dx[k];
        int ny = y + dy[k];
        if (nx < 0 || ny < 0 || state[nx][ny] == -1)
          continue;
        if (state[nx][ny] == 0 || state[nx][ny] == 9)
          continue;
        if (state[nx][ny] <= score[nx][ny] && !memo2[nx][ny]) {
          memo2[nx][ny] = 1;
          que.push(P(nx, ny));
        }
      }
    }
    ans++;
  }
  cout << ans << endl;

  return 0;
}

int StrToInt(string s) {
  stringstream ss;
  ss << s;
  int val;
  ss >> val;
  return val;
}
string IntToStr(int i) {
  stringstream ss;
  ss << i;
  return ss.str();
}