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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#define MP make_pair
using namespace std;
long long int sc[1001][1001], ssc[1001][1001];
char c[1001];
int main() {
  int h, w, cnt = 0, ans = 0, n, kfc = 0;
  int x, y;
  pair<int, int> p;
  queue<pair<int, int>> que;
  queue<pair<int, int>> que1;
  scanf("%d%d", &h, &w);
  for (int i = 0; i < h; i++) {
    scanf("%s", c);
    for (int j = 0; j < w; j++) {

      if (c[j] == '.')
        sc[i][j] = -1;
      else
        sc[i][j] = c[j] - 48;
    }
  }
  for (int i = 1; i < h; i++) {
    for (int j = 1; j < w; j++) {
      if (sc[i][j] != -1) {
        for (int a = i - 1; a < i + 2; a++) {
          for (int b = j - 1; b < j + 2; b++) {
            if (sc[a][b] == -1)
              ssc[i][j]++;
          }
        }
        if (sc[i][j] <= ssc[i][j]) {
          que.push(MP(i, j));
          que1.push(MP(i, j));
          kfc++;
        }
      }
    }
  }
  for (int i = 0; i < kfc; i++) {
    x = que.front().first;
    y = que.front().second;
    que.pop();
    que1.pop();
    sc[x][y] = -1;
    que.push(MP(x, y));
    que1.push(MP(x, y));
  }
  while (!que.empty()) {
    while (!que.empty()) {
      x = que.front().first;
      y = que.front().second;
      que.pop();
      for (int a = -1; a < 2; a++) {
        ssc[x - 1][y + a]++;
        ssc[x + 1][y + a]++;
      }
      ssc[x][y - 1]++;
      ssc[x][y + 1]++;
    }
    n = que1.size();
    for (int i = 0; i < n; i++) {
      x = que1.front().first;
      y = que1.front().second;
      que1.pop();
      for (int a = -1; a < 2; a++) {
        if (sc[x - 1][y + a] <= ssc[x - 1][y + a] && sc[x - 1][y + a] != -1) {
          que.push(MP(x - 1, y + a));
          que1.push(MP(x - 1, y + a));
          sc[x - 1][y + a] = -1;
        }
        if (sc[x + 1][y + a] <= ssc[x + 1][y + a] && sc[x + 1][y + a] != -1) {
          que.push(MP(x + 1, y + a));
          que1.push(MP(x + 1, y + a));
          sc[x + 1][y + a] = -1;
        }
      }
      if (sc[x][y - 1] <= ssc[x][y - 1] && sc[x][y - 1] != -1) {
        que.push(MP(x, y - 1));
        que1.push(MP(x, y - 1));
        sc[x][y - 1] = -1;
      }
      if (sc[x][y + 1] <= ssc[x][y + 1] && sc[x][y + 1] != -1) {
        que.push(MP(x, y + 1));
        que1.push(MP(x, y + 1));
        sc[x][y + 1] = -1;
      }
    }
    ans++;
  }
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#define MP make_pair
using namespace std;
long long int sc[1001][1001], ssc[1001][1001];
char c[1001];
int main() {
  int h, w, cnt = 0, ans = 0, n;
  int x, y;
  pair<int, int> p;
  queue<pair<int, int>> que;
  queue<pair<int, int>> que1;
  scanf("%d%d", &h, &w);
  for (int i = 0; i < h; i++) {
    scanf("%s", c);
    for (int j = 0; j < w; j++) {

      if (c[j] == '.')
        sc[i][j] = -1;
      else
        sc[i][j] = c[j] - 48;
    }
  }
  for (int i = 1; i < h; i++) {
    for (int j = 1; j < w; j++) {
      if (sc[i][j] != -1) {
        for (int a = i - 1; a < i + 2; a++) {
          for (int b = j - 1; b < j + 2; b++) {
            if (sc[a][b] == -1)
              ssc[i][j]++;
          }
        }
        if (sc[i][j] <= ssc[i][j])
          que.push(MP(i, j));
        else if (sc[i][j] != 9)
          que1.push(MP(i, j));
      }
    }
  }
  while (!que.empty()) {
    while (!que.empty()) {
      x = que.front().first;
      y = que.front().second;
      que.pop();
      for (int a = -1; a < 2; a++) {
        ssc[x - 1][y + a]++;
        ssc[x + 1][y + a]++;
      }
      ssc[x][y - 1]++;
      ssc[x][y + 1]++;
    }
    n = que1.size();
    for (int i = 0; i < n; i++) {
      x = que1.front().first;
      y = que1.front().second;
      que1.pop();
      if (sc[x][y] <= ssc[x][y])
        que.push(MP(x, y));
      else if (sc[x][y] != 9)
        que1.push(MP(x, y));
    }
    ans++;
  }
  printf("%d\\n", ans);
  return 0;
}