Problem p00481 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;

int main() {
  int h, w, n;
  int x[10], y[10];
  string map[1000];
  int dmap[1000][1000];
  queue<int> qx, qy;
  int dis;

  cin >> h >> w >> n;

  for (int i = 0; i < h; i++)
    cin >> map[i];

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (map[i][j] == 'S') {
        x[0] = j;
        y[0] = i;
        break;
      }
      if (map[i][j] != 'X' && map[i][j] != '.') {
        x[map[i][j] - '0'] = j;
        y[map[i][j] - '0'] = i;
      }
    }
  }

  dis = 0;
  for (int i = 0; i < n; i++) {
    while (!qx.empty())
      qx.pop();
    while (!qy.empty())
      qy.pop();
    for (int k = 0; k < h; k++) {
      for (int j = 0; j < w; j++) {
        dmap[k][j] = 10000000;
      }
    }
    dmap[y[i]][x[i]] = 0;
    qx.push(x[i]);
    qy.push(y[i]);

    while (!qx.empty()) {
      if (qx.front() > 0 && map[qy.front()][qx.front() - 1] != 'X' &&
          dmap[qy.front()][qx.front() - 1] > dmap[qy.front()][qx.front()] + 1) {
        dmap[qy.front()][qx.front() - 1] = dmap[qy.front()][qx.front()] + 1;
        qx.push(qx.front() - 1);
        qy.push(qy.front());
      }
      if (qy.front() > 0 && map[qy.front() - 1][qx.front()] != 'X' &&
          dmap[qy.front() - 1][qx.front()] > dmap[qy.front()][qx.front()] + 1) {
        dmap[qy.front() - 1][qx.front()] = dmap[qy.front()][qx.front()] + 1;
        qx.push(qx.front());
        qy.push(qy.front() - 1);
      }
      if (qx.front() < w - 1 && map[qy.front()][qx.front() + 1] != 'X' &&
          dmap[qy.front()][qx.front() + 1] > dmap[qy.front()][qx.front()] + 1) {
        dmap[qy.front()][qx.front() + 1] = dmap[qy.front()][qx.front()] + 1;
        qx.push(qx.front() + 1);
        qy.push(qy.front());
      }
      if (qy.front() < h - 1 && map[qy.front() + 1][qx.front()] != 'X' &&
          dmap[qy.front() + 1][qx.front()] > dmap[qy.front()][qx.front()] + 1) {
        dmap[qy.front() + 1][qx.front()] = dmap[qy.front()][qx.front()] + 1;
        qx.push(qx.front());
        qy.push(qy.front() + 1);
      }
      qx.pop();
      qy.pop();
    }

    dis += dmap[y[i + 1]][x[i + 1]];
  }

  cout << dis << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;

int main() {
  int h, w, n;
  int x[10], y[10];
  string map[1000];
  int dmap[1000][1000];
  queue<int> qx, qy;
  int dis;

  cin >> h >> w >> n;

  for (int i = 0; i < h; i++)
    cin >> map[i];

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (map[i][j] == 'S') {
        x[0] = j;
        y[0] = i;
      }
      if (map[i][j] != 'X' && map[i][j] != '.') {
        x[map[i][j] - '0'] = j;
        y[map[i][j] - '0'] = i;
      }
    }
  }

  dis = 0;
  for (int i = 0; i < n; i++) {
    while (!qx.empty())
      qx.pop();
    while (!qy.empty())
      qy.pop();
    for (int k = 0; k < h; k++) {
      for (int j = 0; j < w; j++) {
        dmap[k][j] = 10000000;
      }
    }
    dmap[y[i]][x[i]] = 0;
    qx.push(x[i]);
    qy.push(y[i]);

    while (!qx.empty()) {
      if (qx.front() > 0 && map[qy.front()][qx.front() - 1] != 'X' &&
          dmap[qy.front()][qx.front() - 1] > dmap[qy.front()][qx.front()] + 1) {
        dmap[qy.front()][qx.front() - 1] = dmap[qy.front()][qx.front()] + 1;
        qx.push(qx.front() - 1);
        qy.push(qy.front());
      }
      if (qy.front() > 0 && map[qy.front() - 1][qx.front()] != 'X' &&
          dmap[qy.front() - 1][qx.front()] > dmap[qy.front()][qx.front()] + 1) {
        dmap[qy.front() - 1][qx.front()] = dmap[qy.front()][qx.front()] + 1;
        qx.push(qx.front());
        qy.push(qy.front() - 1);
      }
      if (qx.front() < w - 1 && map[qy.front()][qx.front() + 1] != 'X' &&
          dmap[qy.front()][qx.front() + 1] > dmap[qy.front()][qx.front()] + 1) {
        dmap[qy.front()][qx.front() + 1] = dmap[qy.front()][qx.front()] + 1;
        qx.push(qx.front() + 1);
        qy.push(qy.front());
      }
      if (qy.front() < h - 1 && map[qy.front() + 1][qx.front()] != 'X' &&
          dmap[qy.front() + 1][qx.front()] > dmap[qy.front()][qx.front()] + 1) {
        dmap[qy.front() + 1][qx.front()] = dmap[qy.front()][qx.front()] + 1;
        qx.push(qx.front());
        qy.push(qy.front() + 1);
      }
      qx.pop();
      qy.pop();
    }

    dis += dmap[y[i + 1]][x[i + 1]];
  }

  cout << dis << endl;
}