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

#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int sand[1005][1005];
bool used[1005][1005];
const int MAX = 1007;
queue<int> q1, q2;

int main() {
  string sa;
  int i, j, k, l;
  int n, m;
  int a, b, c, d;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> sa;
    for (j = 0; j < m; j++) {
      if (sa[j] == '.') {
        for (k = -1; k <= 1; k++) {
          for (l = -1; l <= 1; l++) {
            if (i + k > 0 && i + k < n - 1 && j + l > 0 && j + l < m - 1) {
              if (used[i + k][j + l])
                continue;
              used[i + k][j + l] = true;
              q1.push((i + k) * MAX + (j + l));
            }
          }
        }
        continue;
      }
      for (char ca = '1', k = 1; k < 10; k++, ca++) {
        if (sa[j] == ca) {
          sand[i][j] = k;
          break;
        }
      }
    }
  }
  memset(used, false, sizeof(used));
  for (i = 0;; i++) {
    a = q1.size();
    for (j = 0; j < a; j++) {
      c = q1.front(), q1.pop();
      b = c / MAX, c %= MAX;
      if (sand[b][c] == 0)
        continue;
      if (used[b][c])
        continue;
      d = 0;
      for (k = -1; k <= 1; k++) {
        for (l = -1; l <= 1; l++) {
          if (sand[b + k][c + l] == 0)
            d++;
        }
      }
      if (d < sand[b][c])
        continue;
      used[b][c] = true;
      q2.push(b * MAX + c);
      for (k = -1; k <= 1; k++) {
        for (l = -1; l <= 1; l++) {
          if (sand[b + k][c + l] == 0)
            continue;
          q1.push((b + k) * MAX + (c + l));
        }
      }
    }
    if (q2.empty())
      break;
    while (!q2.empty()) {
      c = q2.front(), q2.pop();
      b = c / MAX, c %= MAX;
      sand[b][c] = 0;
    }
  }
  cout << i << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
using namespace std;
int sand[1005][1005];
const int MAX = 1007;
queue<int> q1, q2;

int main() {
  string sa;
  int i, j, k, l;
  int n, m;
  int a, b, c, d;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> sa;
    for (j = 0; j < m; j++) {
      if (sa[j] == '.') {
        for (k = -1; k <= 1; k++) {
          for (l = -1; l <= 1; l++) {
            if (i + k > 0 && i + k < n - 1 && j + l > 0 && j + l < m - 1) {
              q1.push((i + k) * MAX + (j + l));
            }
          }
        }
        continue;
      }
      for (char ca = '1', k = 1; k < 10; k++, ca++) {
        if (sa[j] == ca) {
          sand[i][j] = k;
          break;
        }
      }
    }
  }
  for (i = 0;; i++) {
    a = q1.size();
    for (j = 0; j < a; j++) {
      c = q1.front(), q1.pop();
      b = c / MAX, c %= MAX;
      if (sand[b][c] == 0)
        continue;
      d = 0;
      for (k = -1; k <= 1; k++) {
        for (l = -1; l <= 1; l++) {
          if (sand[b + k][c + l] == 0)
            d++;
        }
      }
      if (d < sand[b][c])
        continue;
      q2.push(b * MAX + c);
      for (k = -1; k <= 1; k++) {
        for (l = -1; l <= 1; l++) {
          if (sand[b + k][c + l] == 0)
            continue;
          q1.push((b + k) * MAX + (c + l));
        }
      }
    }
    if (q2.empty())
      break;
    while (!q2.empty()) {
      c = q2.front(), q2.pop();
      b = c / MAX, c %= MAX;
      sand[b][c] = 0;
    }
  }
  cout << i << endl;
  return 0;
}