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

#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; ++i)

int a[61][61];
int d[2][61][61];

int dx[2][9] = {{-2, -1, -1, 0, 0, 0, 1, 1, 2}, {-2, -1, -1, 0, 0, 0, 1, 1, 2}};
int dy[2][9] = {{1, 1, 2, 1, 2, 3, 1, 2, 1},
                {-1, -1, -2, -1, -2, -3, -1, -2, -1}};

struct node {
  bool left;
  int dist;
  int x, y;
  node(bool l, int d, int x, int y) : left(l), dist(d), x(x), y(y) { ; }
};

bool operator<(const node &n, const node &m) { return n.dist > m.dist; }

int main() {
  int w, h;
  while (cin >> w >> h) {
    if (h == 0 && w == 0)
      break;

    rep(i, 61) rep(j, 61) d[0][i][j] = d[1][i][j] = 1 << 29;
    string s;
    rep(i, h) rep(j, w) {
      cin >> s;
      if (s[0] >= '0' && s[0] <= '9')
        a[i][j] = s[0] - '0';
      else if (s[0] == 'X')
        a[i][j] = -1;
      else if (s[0] == 'S')
        a[i][j] = -2;
      else
        a[i][j] = -3;
    }

    // rep(i,h) {
    //     rep(j,w) cout<<a[i][j]<<" ";
    //     cout<<endl;
    // }

    priority_queue<node> q;
    rep(i, h) rep(j, w) {
      if (a[i][j] == -2) {
        q.push(node(1, 0, i, j));
        q.push(node(0, 0, i, j));
        d[0][i][j] = d[1][i][j] = 0;
      }
    }

    while (!q.empty()) {
      int x = q.top().x, y = q.top().y;
      int di = q.top().dist, leg = q.top().left ^ 1;
      q.pop();

      rep(i, 9) {
        int nx = x + dx[leg][i], ny = y + dy[leg][i];
        if (nx < 0 || nx >= h || ny < 0 || ny > w - 1)
          continue;
        if (a[nx][ny] == -1)
          continue;
        int pp = (a[nx][ny] == -2 || a[nx][ny] == -3) ? 0 : a[nx][ny];
        if (d[leg][nx][ny] > di + pp) {
          d[leg][nx][ny] = di + pp;
          q.push(node(leg, di + pp, nx, ny));
        }
      }
    }

    int ans = 1 << 29;
    rep(i, h) rep(j, w) {
      if (a[i][j] != -3)
        continue;
      ans = min(ans, min(d[0][i][j], d[1][i][j]));
    }

    cout << ((ans == (1 << 29)) ? -1 : ans) << endl;
  }
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)

const int inf = 1 << 29;
int mp[31][61];
int dp[2][31][61];

int movx[2][9] = {{1, 1, 1, 1, 1, 2, 2, 2, 3},
                  {-3, -2, -2, -2, -1, -1, -1, -1, -1}};
int movy[2][9] = {{-2, -1, 0, 1, 2, -1, 0, 1, 0},
                  {0, -1, 0, -1, -2, -1, 0, 1, 2}};

struct State {
  int leg;
  int x, y;
  State(int leg, int y, int x) : leg(leg), x(x), y(y) { ; }
};

int main() {
  int w, h;
  string s;
  while (cin >> w >> h, w | h) {
    memset(mp, 0, sizeof(mp));
    REP(i, 2) REP(j, 31) REP(k, 61) dp[i][j][k] = inf;
    REP(i, h) {
      REP(j, w) {
        cin >> s;
        if (s[0] >= '0' && s[0] <= '9')
          mp[i][j] = s[0] - '0';
        else if (s[0] == 'S')
          mp[i][j] = 0; // START
        else if (s[0] == 'T')
          mp[i][j] = -1; // GOAL
        else
          mp[i][j] = -2; // X
      }
    }

    queue<State> Q;
    REP(i, h) REP(j, w) if (mp[i][j] == 0) {
      Q.push(State(0, i, j));
      Q.push(State(1, i, j));
      dp[0][i][j] = dp[1][i][j] = 0;
    }

    while (!Q.empty()) {
      int l = Q.front().leg, y = Q.front().y, x = Q.front().x;
      Q.pop();
      // cout<<((l==0) ? "left" : "right")<<" "<<y<<" "<<x<<endl;
      int nw = dp[l][y][x];
      for (int i = 0; i < 9; ++i) {
        int dx = x + movx[l][i], dy = y + movy[l][i];
        if (dx < 0 || dx >= w || dy < 0 || dy >= h)
          continue;
        if (mp[dy][dx] == -2)
          continue;
        if (dp[(l + 1) % 2][dy][dx] > nw + max(mp[dy][dx], 0)) {
          dp[(l + 1) % 2][dy][dx] = nw + max(mp[dy][dx], 0);
          Q.push(State((l + 1) % 2, dy, dx));
        }
      }
    }

    int ans = inf;
    REP(i, h) {
      REP(j, w) {
        if (mp[i][j] == -1) {
          ans = min(dp[0][i][j], ans);
          ans = min(dp[1][i][j], ans);
        }
      }
    }
    if (ans == inf)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}