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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#define fs first
#define sc second
using namespace std;

typedef pair<int, int> P1;
typedef pair<P1, P1> P2;

int main() {
  bool t[20][20][4][101];
  int s[20][20], c[20][20][20][20];
  int i, k, m, n, ns, nc, nj, sw, sh, gw, gh;
  char a, b, z;

  while (cin >> m >> n /*scanf(" %d%d ",&m,&n)*/ && (m || n)) {
    cin >> k >> ns;
    // scanf(" %d%d ",&k,&ns);
    fill(&c[0][0][0][0], &c[m - 1][n - 1][m - 1][n - 1] + 1, k);

    fill(&s[0][0], &s[m - 1][n - 1] + 1, 0);
    for (i = 0; i < ns; i++) {
      cin >> a >> z >> sw >> k;
      // scanf(" %c%c%d%d ",&a,&z,&sw,&k);
      s[a - 'a'][sw - 1] = k;
    }

    scanf(" %d ", &nc);
    for (i = 0; i < nc; i++) {
      cin >> a >> z >> sw >> b >> z >> gw;
      // scanf(" %c%c%d %c%c%d ",&a,&z,&sw,&b,&z,&gw);
      c[a - 'a'][sw - 1][b - 'a'][gw - 1] =
          c[b - 'a'][gw - 1][a - 'a'][sw - 1] = -1;
    }

    scanf(" %d ", &nj);
    for (i = 0; i < nj; i++) {
      cin >> a >> z >> sw >> b >> z >> gw >> k;
      // scanf(" %c%c%d %c%c%d%d ",&a,&z,&sw,&b,&z,&gw,&k);
      c[a - 'a'][sw - 1][b - 'a'][gw - 1] += k;
      c[b - 'a'][gw - 1][a - 'a'][sw - 1] += k;
    }

    cin >> a >> z >> sw >> b >> z >> gw;
    // scanf(" %c%c%d %c%c%d ",&a,&z,&sw,&b,&z,&gw);
    sw--;
    gw--;
    sh = a - 'a';
    gh = b - 'a';

    fill(&t[0][0][0][0], &t[m - 1][n - 1][4][101] + 1, false);
    t[sh][sw][0][0] = true;
    queue<P2> q;
    q.push(P2(P1(0, 0), P1(sh, sw)));
    int ans = 1 << 30;

    while (q.size()) {
      P2 p = q.front();
      q.pop();
      int y = p.sc.fs, x = p.sc.sc;

      for (int dd = 0; dd < 4; dd++) {
        if (dd == (p.fs.sc + 2) % 4)
          continue;
        int dy = y, dx = x, dt = p.fs.fs;
        if (dd == 0)
          dx++;
        if (dd == 1)
          dy++;
        if (dd == 2)
          dx--;
        if (dd == 3)
          dy--;
        if (dy < 0 || dx < 0 || m <= dy || n <= dx)
          continue;
        if (c[y][x][dy][dx] < 0)
          continue;
        dt += c[y][x][dy][dx];
        if (dt > 100)
          continue;
        k = s[dy][dx];
        if (k && (((dt / k) & 1) == (dd & 1)))
          continue;
        if (!t[dy][dx][dd][dt]) {
          if (dy == gh && dx == gw)
            ans = min(ans, dt);
          t[dy][dx][dd][dt] = true;
          ;
          q.push(P2(P1(dt, dd), P1(dy, dx)));
        }
      }
    }
    printf("%d\\n", ans);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#define fs first
#define sc second
using namespace std;

typedef pair<int, int> P1;
typedef pair<P1, P1> P2;

int main() {
  bool t[20][20][4][101];
  int s[20][20], c[20][20][20][20];
  int i, k, m, n, ns, nc, nj, sw, sh, gw, gh, y, x;
  char a, b, z;

  while (cin >> m >> n /*scanf(" %d%d ",&m,&n)*/ && (m || n)) {
    cin >> k >> ns;
    // scanf(" %d%d ",&k,&ns);
    fill(&c[0][0][0][0], &c[m - 1][n - 1][m - 1][n - 1] + 1, k);

    fill(&s[0][0], &s[m - 1][n - 1] + 1, 0);
    for (i = 0; i < ns; i++) {
      cin >> a >> z >> sw >> k;
      // scanf(" %c%c%d%d ",&a,&z,&sw,&k);
      s[a - 'a'][sw - 1] = k;
    }

    scanf(" %d ", &nc);
    for (i = 0; i < nc; i++) {
      cin >> a >> z >> sw >> b >> z >> gw;
      // scanf(" %c%c%d %c%c%d ",&a,&z,&sw,&b,&z,&gw);
      c[a - 'a'][sw - 1][b - 'a'][gw - 1] =
          c[b - 'a'][gw - 1][a - 'a'][sw - 1] = -1;
    }

    scanf(" %d ", &nj);
    for (i = 0; i < nj; i++) {
      cin >> a >> z >> sw >> b >> z >> gw >> k;
      // scanf(" %c%c%d %c%c%d%d ",&a,&z,&sw,&b,&z,&gw,&k);
      c[a - 'a'][sw - 1][b - 'a'][gw - 1] += k;
      c[b - 'a'][gw - 1][a - 'a'][sw - 1] += k;
    }

    cin >> a >> z >> sw >> b >> z >> gw;
    // scanf(" %c%c%d %c%c%d ",&a,&z,&sw,&b,&z,&gw);
    sw--;
    gw--;
    sh = a - 'a';
    gh = b - 'a';

    fill(&t[0][0][0][0], &t[m - 1][n - 1][4][101] + 1, false);
    t[sh][sw][0][0] = true;
    queue<P2> q;
    q.push(P2(P1(0, 0), P1(sh, sw)));
    int ans = 1 << 30;

    while (q.size()) {
      P2 p = q.front();
      q.pop();
      y = p.sc.fs;
      x = p.sc.sc;

      for (int dd = 0; dd < 4; dd++) {
        if (dd == (p.fs.sc + 2) % 4)
          continue;
        int dy = y, dx = x, dt = p.fs.fs;
        if (dd == 0)
          dx++;
        if (dd == 1)
          dy++;
        if (dd == 2)
          dx--;
        if (dd == 3)
          dy--;
        if (dy < 0 || dx < 0 || m <= dy || n <= dx)
          continue;
        if (c[y][x][dy][dx] < 0)
          continue;
        dt += c[y][x][dy][dx];
        k = s[dy][dx];
        if (k && (((dt / k) & 1) == (dd & 1)))
          continue;
        if (!t[dy][dx][dd][dt]) {
          if (dy == gh && dx == gw)
            ans = min(ans, dt);
          t[dy][dx][dd][dt] = true;
          ;
          q.push(P2(P1(dt, dd), P1(dy, dx)));
        }
      }
    }
    printf("%d\\n", ans);
  }
}