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

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
struct state {
  int y, x, a, b, c, d, e, f, cost;
  bool operator<(const state &p) const { return cost > p.cost; }
};

int h, w;
int t[10][10];

int d[10][10][6][6][6][6][6][6];

void solve() {
  int ay, ax, by, bx;
  cin >> ay >> ax >> by >> bx;
  // memset(d,sizeof(d),0);
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++)
      for (int k = 0; k < 6; k++)
        for (int l = 0; l < 6; l++)
          for (int m = 0; m < 6; m++)
            for (int n = 0; n < 6; n++)
              for (int o = 0; o < 6; o++)
                for (int p = 0; p < 6; p++)
                  d[i][j][k][l][m][n][o][p] = 0;
  d[ay][ax][0][1][2][3][4][5] = 1;
  priority_queue<state> Q;
  Q.push((state){ay, ax, 0, 1, 2, 3, 4, 5, 1});
  while (!Q.empty()) {
    state s = Q.top();
    Q.pop();
    if (s.cost > d[s.y][s.x][s.a][s.b][s.c][s.d][s.e][s.f])
      continue;
    if (s.y == by && s.x == bx) {
      cout << s.cost - 1 << endl;
      return;
    }
    if (s.y > 0) {
      int *k = &d[s.y - 1][s.x][s.b][s.f][s.c][s.d][s.a][s.e];
      int ncost = s.cost + (s.e + 1) * t[s.y - 1][s.x];
      if (*k == 0 || ncost < *k) {
        *k = ncost;
        Q.push((state){s.y - 1, s.x, s.b, s.f, s.c, s.d, s.a, s.e, ncost});
      }
    }
    if (s.y + 1 < h) {
      int *k = &d[s.y + 1][s.x][s.e][s.a][s.c][s.d][s.f][s.b];
      int ncost = s.cost + (s.b + 1) * t[s.y + 1][s.x];
      if (*k == 0 || ncost < *k) {
        *k = ncost;
        Q.push((state){s.y + 1, s.x, s.e, s.a, s.c, s.d, s.f, s.b, ncost});
      }
    }
    if (s.x > 0) {
      int *k = &d[s.y][s.x - 1][s.c][s.b][s.f][s.a][s.e][s.d];
      int ncost = s.cost + (s.d + 1) * t[s.y][s.x - 1];
      if (*k == 0 || ncost < *k) {
        *k = ncost;
        Q.push((state){s.y, s.x - 1, s.c, s.b, s.f, s.a, s.e, s.d, ncost});
      }
    }
    if (s.x + 1 < w) {
      int *k = &d[s.y][s.x + 1][s.d][s.b][s.a][s.f][s.e][s.c];
      int ncost = s.cost + (s.c + 1) * t[s.y][s.x + 1];
      if (*k == 0 || ncost < *k) {
        *k = ncost;
        Q.push((state){s.y, s.x + 1, s.d, s.b, s.a, s.f, s.e, s.c, ncost});
      }
    }
  }
  cout << "NA" << endl;
}

int main() {
  while (1) {
    cin >> h >> w;
    if (h == 0 && w == 0)
      break;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> t[i][j];
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
struct state {
  int y, x, a, b, c, d, e, f, cost;
  bool operator<(const state &p) const { return cost > p.cost; }
};

int h, w;
int t[10][10];

int d[10][10][6][6][6][6][6][6];

void solve() {
  int ay, ax, by, bx;
  cin >> ay >> ax >> by >> bx;
  //  memset(d,sizeof(d),0);
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++)
      for (int k = 0; k < 6; k++)
        for (int l = 0; l < 6; l++)
          for (int m = 0; m < 6; m++)
            for (int n = 0; n < 6; n++)
              for (int o = 0; o < 6; o++)
                for (int p = 0; p < 6; p++)
                  d[i][j][k][l][m][n][o][p] = 0;

  d[ay][ax][1][2][3][4][5][6] = 1;
  priority_queue<state> Q;
  Q.push((state){ay, ax, 1, 2, 3, 4, 5, 6, 1});
  while (!Q.empty()) {
    state s = Q.top();
    Q.pop();
    if (s.cost > d[s.y][s.x][s.a][s.b][s.c][s.d][s.e][s.f])
      continue;
    if (s.y == by && s.x == bx) {
      cout << s.cost - 1 << endl;
      return;
    }
    if (s.y > 0) {
      int *k = &d[s.y - 1][s.x][s.b][s.f][s.c][s.d][s.a][s.e];
      int ncost = s.cost + s.e * t[s.y - 1][s.x];
      if (*k == 0 || ncost < *k) {
        *k = ncost;
        Q.push((state){s.y - 1, s.x, s.b, s.f, s.c, s.d, s.a, s.e, ncost});
      }
    }
    if (s.y + 1 < h) {
      int *k = &d[s.y + 1][s.x][s.e][s.a][s.c][s.d][s.f][s.b];
      int ncost = s.cost + s.b * t[s.y + 1][s.x];
      if (*k == 0 || ncost < *k) {
        *k = ncost;
        Q.push((state){s.y + 1, s.x, s.e, s.a, s.c, s.d, s.f, s.b, ncost});
      }
    }
    if (s.x > 0) {
      int *k = &d[s.y][s.x - 1][s.c][s.b][s.f][s.a][s.e][s.d];
      int ncost = s.cost + s.d * t[s.y][s.x - 1];
      if (*k == 0 || ncost < *k) {
        *k = ncost;
        Q.push((state){s.y, s.x - 1, s.c, s.b, s.f, s.a, s.e, s.d, ncost});
      }
    }
    if (s.x + 1 < w) {
      int *k = &d[s.y][s.x + 1][s.d][s.b][s.a][s.f][s.e][s.c];
      int ncost = s.cost + s.c * t[s.y][s.x + 1];
      if (*k == 0 || ncost < *k) {
        *k = ncost;
        Q.push((state){s.y, s.x + 1, s.d, s.b, s.a, s.f, s.e, s.c, ncost});
      }
    }
  }
  cout << "NA" << endl;
}

int main() {
  while (1) {
    cin >> h >> w;
    if (h == 0 && w == 0)
      break;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> t[i][j];
    solve();
  }
  return 0;
}