Problem p01401 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define FOR(v, c) for (auto &v : c)

#define ITER(c) __typeof((c).begin())
#define EACH(it, c) for (ITER(c) it = c.begin(); it != c.end(); ++it)
#define ALL(c) (c).begin(), (c).end()

#define PB(n) push_back(n)
#define MP(a, b) make_pair((a), (b))
#define EXIST(c, e) ((c).find(e) != (c).end())

#define fst first
#define snd second

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \
  cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl

int distance(const PII &a, const PII &b) {
  return abs(a.fst - b.fst) + abs(a.snd - b.snd);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    int w, h;
    cin >> w >> h;

    if (!(w | h)) {
      break;
    }

    vector<VS> board(h, VS(w));
    FOR(line, board) {
      FOR(word, line) { cin >> word; }
    }

    int n = 0, maxsize = 0;
    PII s, g;
    map<int, vector<PII>> index;
    REP(i, 0, h) {
      REP(j, 0, w) {
        if (board[i][j] == ".") {
          continue;
        } else if (board[i][j] == "S") {
          s = MP(i, j);
        } else if (board[i][j] == "G") {
          g = MP(i, j);
        } else {
          ISS iss(board[i][j]);
          int num;
          iss >> num;

          index[num].PB(MP(i, j));
          n = max(n, num);
          maxsize = max(maxsize, (int)index[num].size());
        }
      }
    }
    index[0].PB(s);
    index[n + 1].PB(g);

    VVI dp(n + 2, VI(maxsize, INT_MAX));
    dp[0][0] = 0;

    REP(i, 0, n + 1) {
      REP(f, 0, index[i].size()) {
        REP(t, 0, index[i + 1].size()) {
          dp[i + 1][t] = min(dp[i + 1][t],
                             dp[i][f] + distance(index[i][f], index[i + 1][t]));
        }
      }
    }

    cout << dp[n + 1][0] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define FOR(v, c) for (auto &v : c)

#define ITER(c) __typeof((c).begin())
#define EACH(it, c) for (ITER(c) it = c.begin(); it != c.end(); ++it)
#define ALL(c) (c).begin(), (c).end()

#define PB(n) push_back(n)
#define MP(a, b) make_pair((a), (b))
#define EXIST(c, e) ((c).find(e) != (c).end())

#define fst first
#define snd second

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \
  cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl

int distance(const PII &a, const PII &b) {
  return abs(a.fst - b.fst) + abs(a.snd - b.snd);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    int w, h;
    cin >> w >> h;

    if (!(w | h)) {
      break;
    }

    vector<VS> board(h, VS(w));
    FOR(line, board) {
      FOR(word, line) { cin >> word; }
    }

    int n = 0, maxsize = 1;
    PII s, g;
    map<int, vector<PII>> index;
    REP(i, 0, h) {
      REP(j, 0, w) {
        if (board[i][j] == ".") {
          continue;
        } else if (board[i][j] == "S") {
          s = MP(i, j);
        } else if (board[i][j] == "G") {
          g = MP(i, j);
        } else {
          ISS iss(board[i][j]);
          int num;
          iss >> num;

          index[num].PB(MP(i, j));
          n = max(n, num);
          maxsize = max(maxsize, (int)index[num].size());
        }
      }
    }
    index[0].PB(s);
    index[n + 1].PB(g);

    VVI dp(n + 2, VI(maxsize, INT_MAX));
    dp[0][0] = 0;

    REP(i, 0, n + 1) {
      REP(f, 0, index[i].size()) {
        REP(t, 0, index[i + 1].size()) {
          dp[i + 1][t] = min(dp[i + 1][t],
                             dp[i][f] + distance(index[i][f], index[i + 1][t]));
        }
      }
    }

    cout << dp[n + 1][0] << endl;
  }

  return 0;
}