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

//
//  AOJ 0558 Chees
//
//  Created by TaoSama on 2015-02-20
//  Copyright (c) 2014 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define CLR(x, y) memset(x, y, sizeof(x))

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

struct Point {
  int x, y;
  Point(int x = 0, int y = 0) : x(x), y(y) {}
} c[15];
int n, m, k, dp[1005][1005];
int d[4][2] = {-1, 0, 1, 0, 0, 1, 0, -1};
char a[1005][1005];

int bfs(int x) {
  memset(dp, 0x3f, sizeof dp);
  queue<Point> q;
  q.push(c[x - 1]);
  dp[c[x - 1].x][c[x - 1].y] = 0;
  while (!q.empty()) {
    Point cur = q.front();
    q.pop();
    for (int i = 0; i < 4; ++i) {
      Point nxt(cur.x + d[i][0], cur.y + d[i][1]);
      if (nxt.x < 1 || nxt.x > n || nxt.y < 1 || nxt.y > m ||
          a[nxt.x][nxt.y] == 'X')
        continue;
      if (dp[nxt.x][nxt.y] == INF) {
        dp[nxt.x][nxt.y] = dp[cur.x][cur.y] + 1;
        q.push(nxt);
      }
    }
  }
  return dp[c[x].x][c[x].y];
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
  ios_base::sync_with_stdio(0);

  while (cin >> n >> m >> k) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        cin >> a[i][j];
        if (a[i][j] == 'S')
          c[0].x = i, c[0].y = j;
        if (isdigit(a[i][j]))
          c[a[i][j] - '0'].x = i, c[a[i][j] - '0'].y = j;
      }
    }
    int ans = 0;
    for (int i = 1; i <= k; ++i)
      ans += bfs(i);
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define XINF INT_MAX
#define INF 1 << 30
#define MAXN 0x7fffffff
#define eps 1e-8
#define zero(a) fabs(a) < eps
#define sqr(a) ((a) * (a))
#define MP(X, Y) make_pair(X, Y)
#define PB(X) push_back(X)
#define PF(X) push_front(X)
#define REP(X, N) for (int X = 0; X < N; X++)
#define REP2(X, L, R) for (int X = L; X <= R; X++)
#define DEP(X, R, L) for (int X = R; X >= L; X--)
#define CLR(A, X) memset(A, X, sizeof(A))
#define IT iterator
#define PI acos(-1.0)
#define test puts("OK");
typedef long long ll;
typedef pair<int, int> PII;
typedef priority_queue<int, vector<int>, greater<int>> PQI;
typedef vector<PII> VII;
typedef vector<int> VI;
#define X first
#define Y second

char maps[1000 + 10][1000 + 10];
int dis[1000 + 10][1000 + 10];
bool vis[1000 + 10][1000 + 10];
const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
PII fac[20];
int h, w, n;

int bfs(int sx, int sy, int ex, int ey) {
  CLR(vis, 0);
  CLR(dis, 0);
  queue<PII> Q;
  Q.push(MP(sx, sy));
  while (!Q.empty()) {
    PII p = Q.front();
    Q.pop();
    if (p.X == ex && p.Y == ey)
      break;
    vis[p.X][p.Y] = 1;
    REP(i, 4) {
      int tx = p.X + dir[i][0];
      int ty = p.Y + dir[i][1];
      if (tx >= 0 && tx < h && ty >= 0 && ty < w && !vis[tx][ty] &&
          maps[tx][ty] != 'X') {
        Q.push(MP(tx, ty));
        dis[tx][ty] = dis[p.X][p.Y] + 1;
      }
    }
  }
  return dis[ex][ey];
}

int main() {
  ios::sync_with_stdio(false);
  cin >> h >> w >> n;
  REP(i, h)
  REP(j, w) {
    cin >> maps[i][j];
    if (maps[i][j] == 'S') {
      fac[0] = MP(i, j);
      maps[i][j] = '.';
    }
    if (isdigit(maps[i][j]))
      fac[maps[i][j] - '0'] = MP(i, j);
  }
  int ans = 0;
  REP(i, n)
  ans += bfs(fac[i].X, fac[i].Y, fac[i + 1].X, fac[i + 1].Y);
  cout << ans << endl;
  return 0;
}