Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int main() {
  int i, j;
  int a[1002][1002], c[10], d[10], n, w, h, x, y, z;
  int sm = 0;
  char s;
  bool b[1002][1002];
  queue<pair<pair<int, int>, int>> q;
  memset(a, -1, sizeof(a));
  cin >> h >> w >> n;
  for (i = 1; i < h + 1; i++) {
    for (j = 1; j < w + 1; j++) {
      cin >> s;
      if (s != 'X') {
        a[j][i] = 0;
        if (0) {
        } else if (s == '.') {
        } else if (s == 'S') {
          c[0] = j;
          d[0] = i;
        } else {
          c[s - '0'] = j;
          d[s - '0'] = i;
        }
      }
    }
  }
  for (i = 0; i < n; i++) {
    memset(b, 0, sizeof(b));
    while (q.empty() == 0)
      q.pop();
    q.push(make_pair(make_pair(c[i], d[i]), 0));
    for (;;) {
      x = q.front().first.first;
      y = q.front().first.second;
      z = q.front().second;
      q.pop();
      if (a[x][y] != -1 && b[x][y] == 0) {
        if (x == c[i + 1] && y == d[i + 1])
          break;
        b[x][y] = 1;
        q.push(make_pair(make_pair(x - 1, y), z + 1));
        q.push(make_pair(make_pair(x, y - 1), z + 1));
        q.push(make_pair(make_pair(x + 1, y), z + 1));
        q.push(make_pair(make_pair(x, y + 1), z + 1));
      }
    }
    sm += z;
  }
  cout << sm << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
void f(int a[][1002], int b[][1002], int p, int q, int d, int e, int m) {
  if (a[p][q] == -1)
    ;
  else if (p == d && q == e) {
    if (b[p][q] == -1 || b[p][q] > m)
      b[p][q] = m;
  } else if (b[p][q] == -1 || b[p][q] > m) {
    b[p][q] = m;
    f(a, b, p - 1, q, d, e, m + 1);
    f(a, b, p, q - 1, d, e, m + 1);
    f(a, b, p + 1, q, d, e, m + 1);
    f(a, b, p, q + 1, d, e, m + 1);
  }
  return;
}
void g(int a[][1002], int b[][1002], int p, int q, int d, int e, int m) {
  if (a[p][q] == -1)
    ;
  else if (p == d && q == e) {
    if (b[p][q] == -1 || b[p][q] > m)
      b[p][q] = m;
  } else if (b[p][q] == -1 || b[p][q] > m) {
    b[p][q] = m;
    if (d - p)
      g(a, b, p + (d - p > 0 ? 1 : -1), q, d, e, m + 1);
    if (e - q)
      g(a, b, p, q + (e - q > 0 ? 1 : -1), d, e, m + 1);
  }
  return;
}
int main() {
  int i, j;
  int a[1002][1002], b[1002][1002], c[10], d[10], w, h, n;
  int sm = 0;
  memset(a, -1, sizeof(a));
  cin >> h >> w >> n;
  for (i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (j = 0; j < w; j++) {
      if (s[j] == 'X')
        ;
      else {
        a[j + 1][i + 1] = 0;
        if (s[j] == '.')
          ;
        else if (s[j] == 'S') {
          c[0] = j + 1;
          d[0] = i + 1;
        } else {
          c[s[j] - '0'] = j + 1;
          d[s[j] - '0'] = i + 1;
        }
      }
    }
  }
  for (i = 0; i < n; i++) {
    memset(b, -1, sizeof(b));
    g(a, b, c[i], d[i], c[i + 1], d[i + 1], 0);
    if (b[c[i + 1]][d[i + 1]] == -1) {
      memset(b, -1, sizeof(b));
      f(a, b, c[i], d[i], c[i + 1], d[i + 1], 0);
    }
    sm += b[c[i + 1]][d[i + 1]];
  }
  cout << sm << endl;
  return 0;
}