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

#include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <tuple>

using namespace std;

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

char s[100][100];
int d[100][100][2];

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

const int INF = 1e9;

typedef tuple<int, int, int> T;

bool cheak(int x, int y, int mx, int my) {
  return x >= 0 && y >= 0 && x < mx && y < my;
}

int main() {

  int w, h;

  while (cin >> w >> h && w) {

    memset(s, 0, sizeof s);

    rep(i, 100) rep(j, 100) rep(k, 2) d[i][j][k] = INF;

    queue<T> q;
    rep(y, h) rep(x, w) {
      cin >> s[y][x];
      if (s[y][x] == 'S') {
        q.push(T(y, x, 0));
        q.push(T(y, x, 1));
        d[y][x][0] = 0;
        d[y][x][1] = 0;
      }
    }

    while (q.size()) {
      int y = get<0>(q.front());
      int x = get<1>(q.front());
      int r = get<2>(q.front());
      q.pop();

      rep(i, 9) {
        int tx = x + dx[r][i];
        int ty = y + dy[i];

        int tmp = 0;
        if (s[ty][tx] >= '0' && s[ty][tx] <= '9')
          tmp = s[ty][tx] - '0';

        if (cheak(tx, ty, w, h) && (d[ty][tx][1 - r] > d[y][x][r] + tmp) &&
            (s[ty][tx] != 'X')) {

          d[ty][tx][1 - r] = d[y][x][r] + tmp;
          q.push(T(ty, tx, 1 - r));
        }
      }
    }

    int ans = INF;
    rep(y, h) rep(x, w) {
      if (s[y][x] == 'T') {
        ans = min(ans, d[y][x][0]);
        ans = min(ans, d[y][x][1]);
      }
    }

    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <tuple>

using namespace std;

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

char s[100][100];
int d[100][100][2];

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

const int INF = 1e9;

typedef tuple<int, int, int> T;

bool cheak(int x, int y, int mx, int my) {
  return x >= 0 && y >= 0 && x < mx && y < my;
}

int main() {

  int w, h;

  while (cin >> w >> h && w) {

    memset(s, 0, sizeof s);

    rep(i, 100) rep(j, 100) rep(k, 2) d[i][j][k] = INF;

    queue<T> q;
    rep(y, h) rep(x, w) {
      cin >> s[y][x];
      if (s[y][x] == 'S') {
        q.push(T(y, x, 0));
        q.push(T(y, x, 1));
        d[y][x][0] = 0;
        d[y][x][1] = 0;
      }
    }

    while (q.size()) {
      int y = get<0>(q.front());
      int x = get<1>(q.front());
      int r = get<2>(q.front());
      q.pop();

      rep(i, 9) {
        int tx = x + dx[r][i];
        int ty = y + dy[i];

        int tmp = 0;
        if (s[ty][tx] >= '0' && s[ty][tx] <= '9')
          tmp = s[ty][tx] - '0';

        if (cheak(x, y, w, h) && (d[ty][tx][1 - r] > d[y][x][r] + tmp) &&
            (s[ty][tx] != 'X')) {

          d[ty][tx][1 - r] = d[y][x][r] + tmp;
          q.push(T(ty, tx, 1 - r));
        }
      }
    }

    int ans = INF;
    rep(y, h) rep(x, w) {
      if (s[y][x] == 'T') {
        ans = min(ans, d[y][x][0]);
        ans = min(ans, d[y][x][1]);
      }
    }

    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }

  return 0;
}