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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
typedef pair<P, int> T;
char table[1000][1000];
char done[1000][1000];
int H, W, N;
vector<P> cur;
vector<P> nex;
T search(T s, char goal) {
  cur.clear();
  int dist = s.second;
  cur.push_back(s.first);
  int dx[4] = {-1, 0, 0, 1};
  int dy[4] = {0, -1, 1, 0};
  while (true) {
    nex.clear();
    dist++;
    for (int i = 0; i < cur.size(); i++) {
      int x = cur[i].first;
      int y = cur[i].second;
      for (int j = 0; j < 4; j++) {
        int xx = x + dx[j];
        int yy = y + dy[j];
        if (0 <= xx && xx < H && 0 <= yy && yy < W && done[xx][yy] != goal) {
          done[xx][yy] = goal;
          if (table[xx][yy] == 'X') {
          } else if (table[xx][yy] == goal) {
            return T(P(xx, yy), dist);
          } else {
            nex.push_back(P(xx, yy));
          }
        }
      }
    }
    nex.swap(cur);
  }
}
int main() {
  cur.reserve(1000 * 1000);
  nex.reserve(1000 * 1000);
  // cin >> H >> W >> N;
  scanf("%d %d %d", &H, &W, &N);
  T s;
  for (int i = 0; i < H; i++) {
    scanf("%s", table[i]);
    for (int j = 0; j < W; j++) {
      if (table[i][j] == 'S') {
        s = T(P(i, j), 0);
      }
    }
  }
  for (int i = 0; i < N; i++) {
    s = search(s, '0' + i + 1);
  }
  printf("%d\\n", s.second);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
typedef pair<P, int> T;
char table[1000][1000];
bool done[1000][1000];
int H, W, N;
vector<P> cur;
vector<P> nex;
T search(T s, char goal) {
  memset(done, 0, sizeof(done));
  cur.clear();
  int dist = s.second;
  cur.push_back(s.first);
  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};
  while (true) {
    nex.clear();
    dist++;
    for (int i = 0; i < cur.size(); i++) {
      int x = cur[i].first;
      int y = cur[i].second;
      for (int j = 0; j < 4; j++) {
        int xx = x + dx[j];
        int yy = y + dy[j];
        if (0 <= xx && xx < H && 0 <= yy && yy < H && !done[xx][yy]) {
          done[xx][yy] = true;
          if (table[xx][yy] == 'X') {
          } else if (table[xx][yy] == goal) {
            return T(P(xx, yy), dist);
          } else {
            nex.push_back(P(xx, yy));
          }
        }
      }
    }
    nex.swap(cur);
  }
}
int main() {
  cin >> H >> W >> N;
  T s;
  for (int i = 0; i < H; i++) {
    scanf("%s", table[i]);
    for (int j = 0; j < W; j++) {
      if (table[i][j] == 'S') {
        s = T(P(i, j), 0);
      }
    }
  }
  for (int i = 0; i < N; i++) {
    s = search(s, '0' + i + 1);
  }
  cout << s.second << endl;
}