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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#define p pair<int, int>
#define P pair<p, int>
using namespace std;

p x[1000000];
int g[1000][1000];
int main() {
  unordered_map<int, bool> oq;
  queue<P> Q;
  int a, b;
  cin >> a >> b;
  char d[1000];
  for (int c = 0; c < a; c++) {
    scanf("%s", d);
    for (int e = 0; e < b; e++) {
      if (d[e] == '.')
        g[c][e] = 0;
      else
        g[c][e] = d[e] - '0';
    }
  }
  int k = 0;
  for (int f = 1; f < a - 1; f++) {
    for (int h = 1; h < b - 1; h++) {
      if (g[f][h]) {
        int sum = 0;
        for (int i = -1; i < 2; i++) {
          for (int j = -1; j < 2; j++) {
            if (g[f + i][h + j])
              sum++;
          }
        }
        if (9 - sum >= g[f][h] && !oq[f * 10000 + h]) {
          Q.push(P(p(f, h), 1));
          oq[f * 10000 + h] = true;
          x[k] = p(f, h);
          k++;
        }
      }
    }
  }
  for (int t = 0; t < k; t++)
    g[x[t].first][x[t].second] = 0;
  int MAX = 0;
  int w = 0, now = 0;
  while (Q.size()) {
    P s = Q.front();
    Q.pop();
    if (s.second != now) {
      for (int t = 0; t < w; t++) {
        g[x[t].first][x[t].second] = 0;
      }
      w = 0;
      now = s.second;
    }
    MAX = max(MAX, s.second);
    for (int i = -1; i < 2; i++) {
      for (int j = -1; j < 2; j++) {
        if (s.first.first + i >= 1 && s.first.first + i < a - 1 &&
            s.first.second + j >= 1 && s.first.second + j < b - 1 &&
            g[s.first.first + i][s.first.second + j]) {
          int sum = 0;
          for (int n = -1; n < 2; n++) {
            for (int m = -1; m < 2; m++) {
              if (g[s.first.first + i + n][s.first.second + j + m])
                sum++;
            }
          }
          if (9 - sum >= g[s.first.first + i][s.first.second + j] &&
              !oq[(s.first.first + i) * 10000 + s.first.second + j]) {
            Q.push(P(p(s.first.first + i, s.first.second + j), s.second + 1));
            oq[(s.first.first + i) * 10000 + s.first.second + j] = true;
            x[w] = p(s.first.first + i, s.first.second + j);
            w++;
          }
        }
      }
    }
  }
  cout << MAX << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#define p pair<int, int>
#define P pair<p, int>
using namespace std;

p x[1000000];
int g[1000][1000];
int main() {
  queue<P> Q;
  int a, b;
  cin >> a >> b;
  char d[1000];
  for (int c = 0; c < a; c++) {
    scanf("%s", d);
    for (int e = 0; e < b; e++) {
      if (d[e] == '.')
        g[c][e] = 0;
      else
        g[c][e] = d[e] - '0';
    }
  }
  int k = 0;
  for (int f = 1; f < a - 1; f++) {
    for (int h = 1; h < b - 1; h++) {
      if (g[f][h]) {
        int sum = 0;
        for (int i = -1; i < 2; i++) {
          for (int j = -1; j < 2; j++) {
            if (g[f + i][h + j])
              sum++;
          }
        }
        if (9 - sum >= g[f][h]) {
          Q.push(P(p(f, h), 1));
          x[k] = p(f, h);
          k++;
        }
      }
    }
  }
  for (int t = 0; t < k; t++)
    g[x[t].first][x[t].second] = 0;
  int MAX = 0;
  int w = 0, now = 0;
  while (Q.size()) {
    P s = Q.front();
    Q.pop();
    if (s.second != now) {
      for (int t = 0; t < w; t++) {
        g[x[t].first][x[t].second] = 0;
      }
      w = 0;
      now = s.second;
    }
    MAX = max(MAX, s.second);
    for (int i = -1; i < 2; i++) {
      for (int j = -1; j < 2; j++) {
        if (s.first.first + i >= 1 && s.first.first + i < a - 1 &&
            s.first.second + j >= 1 && s.first.second + j < b - 1 &&
            g[s.first.first + i][s.first.second + j]) {
          int sum = 0;
          for (int n = -1; n < 2; n++) {
            for (int m = -1; m < 2; m++) {
              if (g[s.first.first + i + n][s.first.second + j + m])
                sum++;
            }
          }
          if (9 - sum >= g[s.first.first + i][s.first.second + j]) {
            Q.push(P(p(s.first.first + i, s.first.second + j), s.second + 1));
            x[w] = p(s.first.first + i, s.first.second + j);
            w++;
          }
        }
      }
    }
  }
  cout << MAX << endl;
}