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

#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef pair<unsigned short, unsigned short> P;
typedef pair<unsigned char, int> T;
typedef pair<T, P> PP;
#define rep(i, n) for (i = 0; i < n; i++)
#define MAX 1000
char F[MAX][MAX];
char Dx[] = {1, 0, -1, 0}, Dy[] = {0, 1, 0, -1};
unsigned short h, w, n, x, y, nx, ny;
P dist[10];
int bfs() {
  bool used[9][MAX][MAX] = {};
  int c, res = 0, u = 0;
  unsigned char t, i;
  PP p;
  queue<PP> q;
  rep(i, 9) q.push(PP(T(i + '1', 0), dist[i]));
  while (!q.empty()) {
    p = q.front();
    q.pop();
    t = p.first.first;
    if (u & (1 << (t - '1')))
      continue;
    c = p.first.second;
    x = p.second.first;
    y = p.second.second;
    c++;
    rep(i, 4) {
      nx = x + Dx[i];
      ny = y + Dy[i];
      if (nx < w && ny < h && F[ny][nx] != 'X' && !used[t - '1'][ny][nx]) {
        used[t - '1'][ny][nx] = true;
        if (F[ny][nx] == t) {
          u |= (1 << (t - '1'));
          res += c;
        } else {
          q.push(PP(T(t, c), P(nx, ny)));
        }
      }
    }
  }
  return res;
}
int main(void) {
  unsigned short i, j;
  int res;
  scanf("%hd%hd%hd", &h, &w, &n);
  rep(i, h) rep(j, w) {
    scanf(" %c", F[i] + j);
    if (F[i][j] == 'S')
      dist[0] = P(j, i);
    else if (F[i][j] != 'X' && F[i][j] != '.')
      dist[F[i][j] - '0'] = P(j, i);
  }
  res = bfs();
  printf("%d\\n", res);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define START 'S'
#define CANTGO 'X'
#define NONE '.'
#define FINUM field[i][j] - '0'

struct Point {
  int x;
  int y;
};

int dx[] = {0, -1, 0, 1}; // ã©çvñè
int dy[] = {1, 0, -1, 0};
string field[1000];
int mfield[1001][1001] = {};
int life = 1;
int counter = 0;
int csum = 0;
int H, W, N;

int open(vector<Point> &tp) {
  counter++;
  int i, j;
  Point inp;
  int size = tp.size();
  vector<Point>::iterator it;

  for (i = 0; i < size; i++) {
    for (j = 0; j < 4; j++) {
      inp.x = tp[i].x + dx[j];
      inp.y = tp[i].y + dy[j];
      if (inp.x < 0 || inp.y < 0 || inp.x >= H || inp.y >= W)
        continue;
      if (field[inp.x][inp.y] != CANTGO && field[inp.x][inp.y] != START &&
          mfield[inp.x][inp.y] != life) {
        // v·éÉÅZ£Ú®Â\\][
        if (field[inp.x][inp.y] ==
            life + '0') // ©ªÉ©Á½`[YÌ­©
        {
          tp.clear();
          tp.push_back(inp);
          return counter;
        } else {
          tp.push_back(inp);
          mfield[inp.x][inp.y] = life;
        }
      }
    }
  }
  for (i = 0; i < size; i++) {
    it = tp.begin();
    tp.erase(it);
  }
  // copy(tp.begin() , tp.end() , tp.begin());
  return 0;
}

int main(void) {
  int i, j;
  vector<Point> tp;
  Point inp;
  Point gool;
  int count = 0;
  cin >> H >> W >> N;

  for (i = 0; i < H; i++) // üÍ
  {
    cin >> field[i];
    for (j = 0; j < W; j++) {
      if (field[i][j] == 'S') {
        inp.x = i;
        inp.y = j;
        tp.push_back(inp);
        mfield[inp.x][inp.y] = life;
      }
    }
  } // üÍIíè

  while (1) {
    if (open(tp) != 0) {
      life++;
      csum += counter;
      counter = 0;
    }
    if (life > N)
      break;
  }
  cout << csum << endl;

  return 0;
}