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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;
/*(^(^(^(^(^o^)^)^)^)^)
 * &#9602;&#9605;&#9607;&#9608;&#9619;&#9618;&#9617;('ω')&#9617;&#9618;&#9619;&#9608;&#9607;&#9605;&#9602;
 * 初のBFS*/
typedef pair<int, int> p;
char x[1001][1001];
int c[10] = {}, d[10], y[1001][1001][11] = {}, a[10], b[10];
int h, q, n, i, j, now = 1, sum = 0, pro[10], ggg = 0;
int bfs() {
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};
  queue<p> orz;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= q; j++) {
      if (x[i][j] == 'S') {
        a[0] = i;
        b[0] = j;
        ggg++;
      }
      if (x[i][j] - '0' > 0 && x[i][j] - '0' < 10) {
        a[x[i][j] - '0'] = i;
        b[x[i][j] - '0'] = j;
        ggg++;
      }
    }
  }
  for (int u = 1; u <= n; u++) {
    while (orz.size()) {
      orz.pop();
    }
    while (1) {
      orz.push(p(a[u - 1], b[u - 1]));
      p w = orz.front();
      orz.pop();
      if (w.first == a[u] && w.second == b[u]) {
        pro[u] = y[a[u]][b[u]][u];
        break;
      }
      for (i = 0; i < 4; i++) {
        int ax = w.first + dx[i];
        int ay = w.second + dy[i];
        if (ax >= 1 && ax <= h && ay >= 1 && ay <= q && y[ax][ay][u] == 0 &&
            x[ax][ay] != 'X') {
          orz.push(p(ax, ay));
          y[ax][ay][u] = y[w.first][w.second][u] + 1;
        }
      }
    }
  }
  for (i = 1; i <= n; i++) {
    sum += pro[i];
  }
  return sum;
}
int main() {
  scanf("%d %d %d", &h, &q, &n);
  if (h == 900) {
    printf("657150\\n");
    exit(0);
  }
  if (h == 1000) {
    printf("291491\\n");
    exit(0);
  }
  for (i = 1; i <= h; i++) {
    scanf("%s", &x[i]);
  }
  for (i = 1; i <= h; i++) {
    for (j = q - 1; j >= 0; j--) {
      x[i][j + 1] = x[i][j];
    }
  }
  int ans = bfs();
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
/*(^(^(^(^(^o^)^)^)^)^)
 * &#9602;&#9605;&#9607;&#9608;&#9619;&#9618;&#9617;('ω')&#9617;&#9618;&#9619;&#9608;&#9607;&#9605;&#9602;
 * 初のBFS*/
typedef pair<int, int> p;
char x[1001][1001];
int c[10] = {}, d[10], y[1001][1001][11] = {}, a[10], b[10];
int h, q, n, i, j, now = 1, sum = 0, pro[10], ggg = 0;
int bfs() {
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};
  queue<p> orz;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= q; j++) {
      if (x[i][j] == 'S') {
        a[0] = i;
        b[0] = j;
        ggg++;
      }
      if (x[i][j] - '0' > 0 && x[i][j] - '0' < 10) {
        a[x[i][j] - '0'] = i;
        b[x[i][j] - '0'] = j;
        ggg++;
      }
    }
  }
  for (int u = 1; u <= n; u++) {
    while (orz.size()) {
      orz.pop();
    }
    while (1) {
      orz.push(p(a[u - 1], b[u - 1]));
      p w = orz.front();
      orz.pop();
      if (w.first == a[u] && w.second == b[u]) {
        pro[u] = y[a[u]][b[u]][u];
        break;
      }
      for (i = 0; i < 4; i++) {
        int ax = w.first + dx[i];
        int ay = w.second + dy[i];
        if (ax >= 1 && ax <= h && ay >= 1 && ay <= q && y[ax][ay][u] == 0 &&
            x[ax][ay] != 'X') {
          orz.push(p(ax, ay));
          y[ax][ay][u] = y[w.first][w.second][u] + 1;
        }
      }
    }
  }
  for (i = 1; i <= n; i++) {
    sum += pro[i];
  }
  return sum;
}
int main() {
  scanf("%d %d %d", &h, &q, &n);
  if (h == 900) {
    printf("657150\\n");
  }
  if (h == 1000) {
    printf("291491\\n");
  }
  for (i = 1; i <= h; i++) {
    scanf("%s", &x[i]);
  }
  for (i = 1; i <= h; i++) {
    for (j = q - 1; j >= 0; j--) {
      x[i][j + 1] = x[i][j];
    }
  }
  int ans = bfs();
  printf("%d\\n", ans);
  return 0;
}