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

#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P; // y,x

const int dx[] = {1, 0, -1, 0, 0}; // 東、南、西、北、無
const int dy[] = {0, 1, 0, -1, 0};

int gdx[10];
int gdy[10];
int field[22][22];

int cost[22][22][22][22][10];

struct S {
  P m;
  P g;
  int gp;
  S(P m_, P g_, int gp_) : m(m_), g(g_), gp(gp_) {}
};

int main(void) {
  gdx[5] = 0;
  gdx[8] = 0;
  gdx[6] = 1;
  gdx[4] = -1;
  gdx[2] = 0;
  gdy[5] = 0;
  gdy[8] = -1;
  gdy[6] = 0;
  gdy[4] = 0;
  gdy[2] = 1;
  while (1) {
    memset(cost, -1, sizeof(cost));
    memset(field, 0, sizeof(field));
    P ms, gs;
    vector<int> gp;
    int W, H;
    cin >> H >> W;
    if (H == 0 && W == 0)
      return 0;
    for (int y = 1; y <= H; ++y) {
      string in;
      cin >> in;
      for (int x = 1; x <= W; ++x) {
        field[y][x] = in[x - 1];
        if (field[y][x] == 'A') {
          field[y][x] = '.';
          ms.first = y;
          ms.second = x;
        }
        if (field[y][x] == 'B') {
          field[y][x] = '.';
          gs.first = y;
          gs.second = x;
        }
      }
    }
    string in;
    cin >> in;
    for (int i = 0; i < in.size(); ++i) {
      gp.push_back(in[i] - '0');
    }
    ////////////////////////////////////////
    queue<S> q;
    q.push(S(ms, gs, 0));
    int turn = 0;
    P g(-1, -1);
    int gt = -1;
    bool isGoal = false;
    cost[ms.first][ms.second][gs.first][gs.second][gp.size() - 1] = 0;
    while (!q.empty()) {
      int size = q.size();
      for (int aaa = 0; aaa < size; ++aaa) {
        S s = q.front();
        q.pop();
        P gn = P(s.g.first + gdy[gp[turn]], s.g.second + gdx[gp[turn]]);
        if (field[gn.first][gn.second] == 0)
          gn = s.g;
        for (int i = 0; i < 5; ++i) {
          P mn = P(s.m.first + dy[i], s.m.second + dx[i]);
          if (cost[mn.first][mn.second][gn.first][gn.second][turn] == -1 &&
              field[mn.first][mn.second] == '.') {
            cost[mn.first][mn.second][gn.first][gn.second][turn] =
                cost[s.m.first][s.m.second][s.g.first][s.g.second]
                    [(turn + gp.size() - 1) % gp.size()] +
                1;
            if (mn == gn) {
              g = mn;
              gt = turn;
            }
            q.push(S(mn, gn, gp[turn]));
          }
        }
        if (g.first != -1)
          break;
      }
      if (g.first != -1)
        break;
      ++turn;
      turn %= gp.size();
    }
    if (g.first != -1)
      cout << cost[g.first][g.second][g.first][g.second][gt] << ' '
           << g.first - 1 << ' ' << g.second - 1 << endl;
    else
      cout << "impossible" << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P; // y,x

const int dx[] = {1, 0, -1, 0, 0}; // 東、南、西、北、無
const int dy[] = {0, 1, 0, -1, 0};

int gdx[10];
int gdy[10];
int field[21][21];

int cost[21][21][21][21][10];

struct S {
  P m;
  P g;
  int gp;
  S(P m_, P g_, int gp_) : m(m_), g(g_), gp(gp_) {}
};

int main(void) {
  gdx[5] = 0;
  gdx[8] = 0;
  gdx[6] = 1;
  gdx[4] = -1;
  gdx[2] = 0;
  gdy[5] = 0;
  gdy[8] = -1;
  gdy[6] = 0;
  gdy[4] = 0;
  gdy[2] = 1;
  while (1) {
    memset(cost, -1, sizeof(cost));
    memset(field, 0, sizeof(field));
    P ms, gs;
    vector<int> gp;
    int W, H;
    cin >> H >> W;
    if (H == 0 && W == 0)
      return 0;
    for (int y = 1; y <= H; ++y) {
      string in;
      cin >> in;
      for (int x = 1; x <= W; ++x) {
        field[y][x] = in[x - 1];
        if (field[y][x] == 'A') {
          field[y][x] = '.';
          ms.first = y;
          ms.second = x;
        }
        if (field[y][x] == 'B') {
          field[y][x] = '.';
          gs.first = y;
          gs.second = x;
        }
      }
    }
    string in;
    cin >> in;
    for (int i = 0; i < in.size(); ++i) {
      gp.push_back(in[i] - '0');
    }
    ////////////////////////////////////////
    queue<S> q;
    q.push(S(ms, gs, 0));
    int turn = 0;
    P g(-1, -1);
    int gt = -1;
    bool isGoal = false;
    cost[ms.first][ms.second][gs.first][gs.second][gp.size() - 1] = 0;
    while (!q.empty()) {
      int size = q.size();
      for (int aaa = 0; aaa < size; ++aaa) {
        S s = q.front();
        q.pop();
        P gn = P(s.g.first + gdy[gp[turn]], s.g.second + gdx[gp[turn]]);
        if (field[gn.first][gn.second] == 0)
          gn = s.g;
        for (int i = 0; i < 5; ++i) {
          P mn = P(s.m.first + dy[i], s.m.second + dx[i]);
          if (cost[mn.first][mn.second][gn.first][gn.second][turn] == -1 &&
              field[mn.first][mn.second] == '.') {
            cost[mn.first][mn.second][gn.first][gn.second][turn] =
                cost[s.m.first][s.m.second][s.g.first][s.g.second]
                    [(turn + gp.size() - 1) % gp.size()] +
                1;
            if (mn == gn) {
              g = mn;
              gt = turn;
            }
            q.push(S(mn, gn, gp[turn]));
          }
        }
        if (g.first != -1)
          break;
      }
      if (g.first != -1)
        break;
      ++turn;
      turn %= gp.size();
    }
    if (g.first != -1)
      cout << cost[g.first][g.second][g.first][g.second][gt] << ' '
           << g.first - 1 << ' ' << g.second - 1 << endl;
    else
      cout << "impossible" << endl;
  }
}