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

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <string.h>

using namespace std;

struct node {
  int s;
  int x, y;
};

int dir[][2] = {0, 1, 1, 0, 0, -1, -1, 0};
int vis[1010][1010];
int n, m, k, sx[20], sy[20];
char mp[1010][1010];

int bfs(int x, int y, int ex, int ey) {
  queue<node> q;
  node a, b, d;
  a.x = x;
  a.y = y;
  a.s = 0;
  vis[x][y] = 1;
  q.push(a);
  while (!q.empty()) {
    b = q.front();
    q.pop();
    if (b.x == ex && b.y == ey) {
      return b.s;
    }
    for (int i = 0; i < 4; i++) {
      d.x = b.x + dir[i][0];
      d.y = b.y + dir[i][1];
      if (d.x >= 0 && d.x < n && d.y >= 0 && d.y < m && vis[d.x][d.y] == 0) {
        if (mp[d.x][d.y] >= '1' && mp[d.x][d.y] <= '9' || mp[d.x][d.y] == '.') {
          vis[d.x][d.y] = 1;
          d.s = b.s + 1;
          q.push(d);
        }
      }
    }
  }
}

int main() {
  while (~scanf("%d%d%d", &n, &m, &k)) {
    int i, j, t;

    for (i = 0; i < n; i++) {
      scanf("%s", mp[i]);
      for (j = 0; j < m; j++)
        if (mp[i][j] == 'S') {
          sx[0] = i;
          sy[0] = j;
          mp[i][j] = '.';
        }
    }
    for (i = 0; i < n; i++)
      for (j = 0; j < m; j++)
        if (mp[i][j] >= '1' && mp[i][j] <= '9') {
          sx[mp[i][j] - '0'] = i;
          sy[mp[i][j] - '0'] = j;
        }
    int ans = 0;
    for (i = 1; i <= k; i++) {
      memset(vis, 0, sizeof(vis));
      ans += bfs(sx[i - 1], sy[i - 1], sx[i], sy[i]);
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int H, W, N;
typedef pair<int, int> P;
#define INF 1e-9
const int MAX_N = 1001;
char maze[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int d[MAX_N][MAX_N];
bool right(int x, int y) {
  if (x >= 0 && x < H && y >= 0 && y < W && maze[x][y] != 'X' && !vis[x][y])
    return true;
  return false;
}
int bfs(int sx, int sy, int gx, int gy) {
  queue<P> que;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      d[i][j] = INF;
  }
  que.push(P(sx, sy));
  d[sx][sy] = 0;
  while (que.size()) {
    P p = que.front();
    que.pop();
    if (p.first == gx && p.second == gy)
      break;
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (right(nx, ny)) {
        que.push(P(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  return d[gx][gy];
}
int main() {
  while (scanf("%d%d%d", &H, &W, &N) != EOF) {
    getchar();
    for (int i = 0; i < H; i++)
      scanf("%s", maze[i]);
    int ans = 0;
    int sx = -1, sy = -1;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++)
        if (maze[i][j] == 'S') {
          sx = i;
          sy = j;
        }
      if (sx != -1 && sy != -1)
        break;
    }
    for (int i = 1; i <= N; i++) {
      memset(vis, false, sizeof(vis));
      int nx = -1, ny = -1;
      for (int j = 0; j < H; j++) {
        for (int k = 0; k < W; k++) {
          if (maze[j][k] == '0' + i) {
            nx = j;
            ny = k;
            break;
          }
        }
        if (nx != -1 && ny != -1)
          break;
      }
      ans += bfs(sx, sy, nx, ny);
      sx = nx;
      sy = ny;
    }
    printf("%d\\n", ans);
  }
  return 0;
}