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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

struct P {
  int lx, ly;
  int rx, ry;
  bool f; // f : ツ篠淞右ツ堕ォ(0) ツ債カツ堕ォ(1)
  int dis;
  P(int lx, int ly, int rx, int ry, bool f, int dis)
      : lx(lx), ly(ly), rx(rx), ry(ry), f(f), dis(dis) {}
};

const bool operator<(const P &a, const P &b) { return a.dis > b.dis; }

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

int dist[30][60][30][60][2];
int main() {
  int w, h;
  while (cin >> w >> h, w || h) {
    char ba[w][h];
    priority_queue<P> Q;
    REP(y, h) {
      REP(x, w) {
        cin >> ba[x][y];
        if (ba[x][y] == 'S') {
          Q.push(P(x, y, x, y, 0, 0));
          Q.push(P(x, y, x, y, 1, 0));
        }
      }
    }

    REP(i, w) REP(j, h) REP(k, w) REP(l, h) REP(m, 2) dist[i][j][k][l][m] = INF;
    //    memset(visited,0,sizeof(visited));
    int res = -1;
    while (!Q.empty()) {
      P p = Q.top();
      Q.pop();
      if (dist[p.lx][p.ly][p.rx][p.ry][p.f] < p.dis)
        continue;

      // printf("%d,%d,%d,%d\\n",p.lx,p.ly,p.rx,p.ry);
      if (ba[p.lx][p.ly] == 'T' || ba[p.rx][p.ry] == 'T') {
        res = p.dis;
        break;
      }
      REP(k, 9) {
        if (p.f) { // ツ債カツ堕ォ
          int xx = p.rx - dx[k];
          int yy = p.ry + dy[k];
          if (xx < 0 || xx >= w || yy < 0 || yy >= h)
            continue;
          // printf("ssdfsdf\\n");
          if (ba[xx][yy] == 'X')
            continue;
          int hoge = 0;
          if (isdigit(ba[xx][yy]))
            hoge = ba[xx][yy] - '0';
          if (dist[xx][yy][p.rx][p.ry][0] > p.dis + hoge) {
            dist[xx][yy][p.rx][p.ry][0] = p.dis + hoge;
            Q.push(P(xx, yy, p.rx, p.ry, 0, p.dis + hoge));
          }
        } else {
          int xx = p.lx + dx[k];
          int yy = p.ly + dy[k];
          if (xx < 0 || xx >= w || yy < 0 || yy >= h)
            continue;
          if (ba[xx][yy] == 'X')
            continue;
          int hoge = 0;
          if (isdigit(ba[xx][yy]))
            hoge = ba[xx][yy] - '0';
          if (dist[p.lx][p.ly][xx][yy][1] > p.dis + hoge) {
            dist[p.lx][p.ly][xx][yy][1] = p.dis + hoge;
            Q.push(P(p.lx, p.ly, xx, yy, 1, p.dis + hoge));
          }
        }
      }
    }
    cout << res << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

struct P {
  int lx, ly;
  int rx, ry;
  bool f; // f : ツ篠淞右ツ堕ォ(0) ツ債カツ堕ォ(1)
  int dis;
  P(int lx, int ly, int rx, int ry, bool f, int dis)
      : lx(lx), ly(ly), rx(rx), ry(ry), f(f), dis(dis) {}
};

const bool operator<(const P &a, const P &b) { return a.dis > b.dis; }

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

int main() {
  int w, h;
  while (cin >> w >> h, w || h) {
    char ba[w][h];
    priority_queue<P> Q;
    REP(y, h) {
      REP(x, w) {
        cin >> ba[x][y];
        if (ba[x][y] == 'S') {
          Q.push(P(x, y, x, y, 0, 0));
          Q.push(P(x, y, x, y, 1, 0));
        }
      }
    }
    int dist[w][h][w][h][2];
    REP(i, w) REP(j, h) REP(k, w) REP(l, h) REP(m, 2) dist[i][j][k][l][m] = INF;
    //    memset(visited,0,sizeof(visited));
    int res = -1;
    while (!Q.empty()) {
      P p = Q.top();
      Q.pop();
      if (dist[p.lx][p.ly][p.rx][p.ry][p.f] < p.dis)
        continue;

      // printf("%d,%d,%d,%d\\n",p.lx,p.ly,p.rx,p.ry);
      if (ba[p.lx][p.ly] == 'T' || ba[p.rx][p.ry] == 'T') {
        res = p.dis;
        break;
      }
      REP(k, 9) {
        if (p.f) { // ツ債カツ堕ォ
          int xx = p.rx - dx[k];
          int yy = p.ry + dy[k];
          if (xx < 0 || xx >= w || yy < 0 || yy >= h)
            continue;
          // printf("ssdfsdf\\n");
          if (ba[xx][yy] == 'X')
            continue;
          int hoge = 0;
          if (isdigit(ba[xx][yy]))
            hoge = ba[xx][yy] - '0';
          if (dist[xx][yy][p.rx][p.ry][0] > p.dis + hoge) {
            dist[xx][yy][p.rx][p.ry][0] = p.dis + hoge;
            Q.push(P(xx, yy, p.rx, p.ry, 0, p.dis + hoge));
          }
        } else {
          int xx = p.lx + dx[k];
          int yy = p.ly + dy[k];
          if (xx < 0 || xx >= w || yy < 0 || yy >= h)
            continue;
          if (ba[xx][yy] == 'X')
            continue;
          int hoge = 0;
          if (isdigit(ba[xx][yy]))
            hoge = ba[xx][yy] - '0';
          if (dist[p.lx][p.ly][xx][yy][1] > p.dis + hoge) {
            dist[p.lx][p.ly][xx][yy][1] = p.dis + hoge;
            Q.push(P(p.lx, p.ly, xx, yy, 1, p.dis + hoge));
          }
        }
      }
    }
    cout << res << endl;
  }
}