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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct _nd {
  int cost;
  int x, y;
  int a, b, c;
};
int main() {
  int h, w;
  const int dx[] = {0, 1, 0, -1}; // up, right, down, left
  const int dy[] = {-1, 0, 1, 0};
  const int inf = 99999999;
  while (cin >> h >> w, h | w) {
    int field[10][10];
    int memo[7][7][7][10][10];
    int sx, sy, gx, gy;
    int ans = inf;

    for (int a = 0; a < 7; a++)
      for (int b = 0; b < 7; b++)
        for (int c = 0; c < 7; c++)
          for (int d = 0; d < 10; d++)
            for (int e = 0; e < 10; e++)
              memo[a][b][c][d][e] = inf;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> field[i][j];

    cin >> sy >> sx;
    cin >> gy >> gx;

    struct _nd nd;
    nd.cost = 0;
    nd.x = sx;
    nd.y = sy;
    nd.a = 1;
    nd.b = 2;
    nd.c = 3;
    vector<struct _nd> vs;
    vs.push_back(nd);
    memo[1][2][3][sy][sx] = 0;
    while (vs.size()) {
      int tmin = inf;
      int ind;
      for (int i = 0; i < vs.size(); i++) {
        if (tmin > vs[i].cost) {
          tmin = vs[i].cost;
          ind = i;
        }
      }
      nd = vs[ind];
      if ((nd.x == gx && nd.y == gy) || nd.cost >= ans)
        break;
      vs.erase(vs.begin() + ind);

      for (int k = 0; k < 4; k++) {
        int tx = nd.x + dx[k];
        int ty = nd.y + dy[k];
        int na = k == 0 ? nd.b : k == 1 ? 7 - nd.c : k == 2 ? 7 - nd.b : nd.c;
        int nb = k == 0 ? 7 - nd.a : k == 1 ? nd.b : k == 2 ? nd.a : nd.b;
        int nc = k == 0 ? nd.c : k == 1 ? nd.a : k == 2 ? nd.c : 7 - nd.a;

        // printf("k:%d  (%d,%d),[na:nb:nc]=[%d:%d:%d]\\n",k,tx,ty,na,nb,nc);
        if (tx < 0 || ty < 0 || tx >= w || ty >= h ||
            memo[na][nb][nc][ty][tx] <= field[ty][tx] * (7 - na) + nd.cost)
          continue;
        // printf("k:%d  (%d,%d),[na:nb:nc]=[%d:%d:%d]
        // _old[%d:%d:%d]\\n",k,tx,ty,na,nb,nc 																															,nd.a,nd.b,nd.c);
        memo[na][nb][nc][ty][tx] = field[ty][tx] * (7 - na) + nd.cost;
        if (tx == gx && ty == gy) {
          ans = min(ans, field[ty][tx] * (7 - na) + nd.cost);
        }
        // printf("memo[%d][%d][%d][%d][%d] =
        // %d\\n",na,nb,nc,ty,tx,field[ty][tx]*(7-na) + nd.cost);
        struct _nd tmp = {
            field[ty][tx] * (7 - na) + nd.cost, tx, ty, na, nb, nc};
        vs.push_back(tmp);
      }
      // printf("_\\n");
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct _nd {
  int cost;
  int x, y;
  int a, b, c;
};
int main() {
  int h, w;
  const int dx[] = {0, 1, 0, -1}; // up, right, down, left
  const int dy[] = {-1, 0, 1, 0};
  const int inf = 99999999;
  while (cin >> h >> w, h | w) {
    int field[2][10];
    int memo[7][7][7][2][10];
    int sx, sy, gx, gy;
    int ans = inf;

    for (int a = 0; a < 7; a++)
      for (int b = 0; b < 7; b++)
        for (int c = 0; c < 7; c++)
          for (int d = 0; d < 2; d++)
            for (int e = 0; e < 10; e++)
              memo[a][b][c][d][e] = inf;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> field[i][j];

    cin >> sy >> sx;
    cin >> gy >> gx;

    struct _nd nd;
    nd.cost = 0;
    nd.x = sx;
    nd.y = sy;
    nd.a = 1;
    nd.b = 2;
    nd.c = 3;
    vector<struct _nd> vs;
    vs.push_back(nd);
    memo[1][2][3][sy][sx] = 0;
    while (vs.size()) {
      int tmin = inf;
      int ind;
      for (int i = 0; i < vs.size(); i++) {
        if (tmin > vs[i].cost) {
          tmin = vs[i].cost;
          ind = i;
        }
      }
      nd = vs[ind];
      if ((nd.x == gx && nd.y == gy) || nd.cost >= ans)
        break;
      vs.erase(vs.begin() + ind);

      for (int k = 0; k < 4; k++) {
        int tx = nd.x + dx[k];
        int ty = nd.y + dy[k];
        int na = k == 0 ? nd.b : k == 1 ? 7 - nd.c : k == 2 ? 7 - nd.b : nd.c;
        int nb = k == 0 ? 7 - nd.a : k == 1 ? nd.b : k == 2 ? nd.a : nd.b;
        int nc = k == 0 ? nd.c : k == 1 ? nd.a : k == 2 ? nd.c : 7 - nd.a;

        // printf("k:%d  (%d,%d),[na:nb:nc]=[%d:%d:%d]\\n",k,tx,ty,na,nb,nc);
        if (tx < 0 || ty < 0 || tx >= w || ty >= h ||
            memo[na][nb][nc][ty][tx] <= field[ty][tx] * (7 - na) + nd.cost)
          continue;
        // printf("k:%d  (%d,%d),[na:nb:nc]=[%d:%d:%d]
        // _old[%d:%d:%d]\\n",k,tx,ty,na,nb,nc 																															,nd.a,nd.b,nd.c);
        memo[na][nb][nc][ty][tx] = field[ty][tx] * (7 - na) + nd.cost;
        if (tx == gx && ty == gy) {
          ans = min(ans, field[ty][tx] * (7 - na) + nd.cost);
        }
        // printf("memo[%d][%d][%d][%d][%d] =
        // %d\\n",na,nb,nc,ty,tx,field[ty][tx]*(7-na) + nd.cost);
        struct _nd tmp = {
            field[ty][tx] * (7 - na) + nd.cost, tx, ty, na, nb, nc};
        vs.push_back(tmp);
      }
      // printf("_\\n");
    }
    printf("%d\\n", ans);
  }
  return 0;
}