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

#include <iostream>
#include <map>
#include <queue>
using namespace std;

struct p {
  int x, y;
};
typedef struct p P;
const int INF = 1e+7;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

int w, h, n;
string m[1001];
P cheese[11];
int ans = 0;
int d[1001][1001];

void bfs() {
  for (int i = 1; i <= n; i++) {
    P sp = cheese[i - 1];
    int sx = cheese[i - 1].x;
    int sy = cheese[i - 1].y;
    int gx = cheese[i].x;
    int gy = cheese[i].y;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        d[y][x] = INF;
      }
    }

    queue<P> q;
    q.push(sp);
    d[sy][sx] = 0;
    while (!q.empty()) {
      P now = q.front();
      q.pop();
      int x = now.x;
      int y = now.y;
      if (x == gx && y == gy) {
        ans += d[gy][gx];
        break;
      }
      for (int i = 0; i < 4; i++) {
        int mx = x + dx[i];
        int my = y + dy[i];
        if (mx < 0 || my < 0 || mx >= w || my >= h)
          continue;
        if (m[my][mx] != 'X') {
          P next;

          next.x = mx;
          next.y = my;
          if (d[my][mx] == INF) {
            d[my][mx] = d[y][x] + 1;
            q.push(next);
          }
        }
      }
    }
  }
}

int main() {
  cin >> h >> w >> n;
  for (int y = 0; y < h; y++) {
    cin >> m[y];
  }

  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      char c = m[y][x];
      if (c == 'S') {
        cheese[0].x = x;
        cheese[0].y = y;
      }
      if (c >= '1' && c <= '9') {
        cheese[c - '0'].x = x;
        cheese[c - '0'].y = y;
      }
    }
  }
  bfs();
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
using namespace std;

typedef pair<int, int> P;
struct State {
  int x, y, cnt;
};
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

int main() {
  int w, h, n;
  string m[1001];
  cin >> h >> w >> n;
  for (int y = 0; y < h; y++) {
    cin >> m[y];
  }

  struct State cheese[11];
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      char c = m[y][x];
      if (c == 'S') {
        cheese[0].x = x;
        cheese[0].y = y;
        cheese[0].cnt = 0;
      }
      if (c >= '1' && c <= '9') {
        cheese[c - '0'].x = x;
        cheese[c - '0'].y = y;
        cheese[c - '0'].cnt = 0;
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    struct State sp = cheese[i - 1];
    struct State gp = cheese[i];
    map<P, bool> memo;
    P p_(sp.x, sp.y);
    memo[p_] = true;

    queue<struct State> q;
    q.push(sp);
    while (!q.empty()) {
      struct State p = q.front();
      q.pop();
      if (p.x == gp.x && p.y == gp.y) {
        ans += p.cnt;
        break;
      }
      P p_(p.x, p.y);
      memo[p_] = true;

      for (int i = 0; i < 4; i++) {
        int mx = p.x + dx[i];
        int my = p.y + dy[i];
        if (mx < 0 || my < 0 || mx >= w || my >> h)
          continue;
        if (m[my][mx] != 'X') {
          struct State next;
          next.x = mx;
          next.y = my;
          next.cnt = p.cnt + 1;
          P p_(mx, my);
          if (memo[p_] != true) {
            q.push(next);
          }
        }
      }
    }
  }
  cout << ans << endl;
}