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

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int INF = 1 << 28;
inline pair<int, int> inputPos() {
  string str;
  cin >> str;
  return pair<int, int>(str[0] - 'a', atoi(str.c_str() + 2) - 1);
}

int m, n;
int D;
int sign[20][20];
int cant[20][20][20][20];
int cst[20][20][20][20];
pair<int, int> sp, gp;

bool ok(int x, int y) { return x >= 0 && x < m && y >= 0 && y < n; }

void solve() {
  static int dist[101][20][20][4];
  fill_n(***dist, 101 * 20 * 20 * 4, INF);
  dist[0][sp.first][sp.second][3] = 0;
  for (int t = 0; t < 100; t++) {
    for (int x = 0; x < m; x++) {
      for (int y = 0; y < n; y++) {
        for (int d = 0; d < 4; d++) {
          if (dist[t][x][y][d] == INF)
            continue;
          for (int dd = 3; dd <= 5; dd++) {
            int dir = (d + dd) % 4;
            int nx = x + dx[dir];
            int ny = y + dy[dir];
            if (!ok(nx, ny))
              continue;
            if (cant[x][y][nx][ny])
              continue;
            int nt = t + D + cst[x][y][nx][ny];
            if (nt > 100)
              continue;
            if (sign[nx][ny]) {
              if ((nt / sign[nx][ny] + dir) % 2 == 1) {
                continue;
              }
            }
            dist[nt][nx][ny][dir] = min(dist[nt][nx][ny][dir], nt);
          }
        }
      }
    }
  }

  for (int t = 0;; t++) {
    for (int d = 0; d < 4; d++) {
      if (dist[t][gp.first][gp.second][d] < INF) {
        printf("%d\\n", dist[t][gp.first][gp.second][d]);
        return;
      }
    }
  }
}

int main() {
  while (scanf("%d %d", &m, &n), m) {
    memset(sign, 0, sizeof(sign));
    memset(cant, 0, sizeof(cant));
    memset(cst, 0, sizeof(cst));

    scanf("%d", &D);

    int ns;
    scanf("%d", &ns);
    for (int i = 0; i < ns; i++) {
      pair<int, int> pos = inputPos();
      int k;
      scanf("%d", &k);
      sign[pos.first][pos.second] = k;
    }

    int nc;
    scanf("%d", &nc);
    for (int i = 0; i < nc; i++) {
      pair<int, int> pos1 = inputPos();
      pair<int, int> pos2 = inputPos();
      cant[pos1.first][pos1.second][pos2.first][pos2.second] = true;
      cant[pos2.first][pos2.second][pos1.first][pos1.second] = true;
    }

    int nj;
    scanf("%d", &nj);
    for (int i = 0; i < nj; i++) {
      pair<int, int> pos1 = inputPos();
      pair<int, int> pos2 = inputPos();
      int d;
      scanf("%d", &d);
      cst[pos1.first][pos1.second][pos2.first][pos2.second] += d;
      cst[pos2.first][pos2.second][pos1.first][pos1.second] += d;
    }

    sp = inputPos();
    gp = inputPos();

    solve();
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int INF = 1 << 28;
inline pair<int, int> inputPos() {
  string str;
  cin >> str;
  return pair<int, int>(str[0] - 'a', atoi(str.c_str() + 2) - 1);
}

int m, n;
int D;
int sign[20][20];
int cant[20][20][20][20];
int cst[20][20][20][20];
pair<int, int> sp, gp;

bool ok(int x, int y) { return x >= 0 && x < m && y >= 0 && y < n; }

void solve() {
  static int dist[101][20][20][4];
  fill_n(***dist, 101 * 20 * 20 * 4, INF);
  dist[0][sp.first][sp.second][3] = 0;
  for (int t = 0; t < 100; t++) {
    for (int x = 0; x < m; x++) {
      for (int y = 0; y < n; y++) {
        for (int d = 0; d < 4; d++) {
          if (dist[t][x][y][d] == INF)
            continue;
          for (int dd = 3; dd <= 5; dd++) {
            int dir = (d + dd) % 4;
            int nx = x + dx[dir];
            int ny = y + dy[dir];
            if (!ok(nx, ny))
              continue;
            if (cant[x][y][nx][ny])
              continue;
            int nt = t + D + cst[x][y][nx][ny];
            if (sign[nx][ny]) {
              if ((nt / sign[nx][ny] + dir) % 2 == 1) {
                continue;
              }
            }
            dist[nt][nx][ny][dir] = min(dist[nt][nx][ny][dir], nt);
          }
        }
      }
    }
  }

  for (int t = 0;; t++) {
    for (int d = 0; d < 4; d++) {
      if (dist[t][gp.first][gp.second][d] < INF) {
        printf("%d\\n", dist[t][gp.first][gp.second][d]);
        return;
      }
    }
  }
}

int main() {
  while (scanf("%d %d", &m, &n), m) {
    memset(sign, 0, sizeof(sign));
    memset(cant, 0, sizeof(cant));
    memset(cst, 0, sizeof(cst));

    scanf("%d", &D);

    int ns;
    scanf("%d", &ns);
    for (int i = 0; i < ns; i++) {
      pair<int, int> pos = inputPos();
      int k;
      scanf("%d", &k);
      sign[pos.first][pos.second] = k;
    }

    int nc;
    scanf("%d", &nc);
    for (int i = 0; i < nc; i++) {
      pair<int, int> pos1 = inputPos();
      pair<int, int> pos2 = inputPos();
      cant[pos1.first][pos1.second][pos2.first][pos2.second] = true;
      cant[pos2.first][pos2.second][pos1.first][pos1.second] = true;
    }

    int nj;
    scanf("%d", &nj);
    for (int i = 0; i < nj; i++) {
      pair<int, int> pos1 = inputPos();
      pair<int, int> pos2 = inputPos();
      int d;
      scanf("%d", &d);
      cst[pos1.first][pos1.second][pos2.first][pos2.second] += d;
      cst[pos2.first][pos2.second][pos1.first][pos1.second] += d;
    }

    sp = inputPos();
    gp = inputPos();

    solve();
  }
}