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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

struct State {
  pair<int, int> t, k;
  int turn;
};

int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
int w, h;
int field[52][52];
bool visited[52][52][52][52];

int main() {
  while (cin >> w >> h, w | h) {
    memset(visited, false, sizeof(visited));
    queue<State> q;
    State a, b;
    a.turn = 0;
    cin >> a.t.second >> a.t.first;
    cin >> a.k.second >> a.k.first;
    for (int i = 0; i < 52; i++) {
      for (int j = 0; j < 52; j++)
        field[i][j] = 1;
    }
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++)
        cin >> field[i][j];
    }
    int ans = -1;
    q.push(a);
    visited[a.t.first][a.t.second][a.k.first][a.k.second] = true;
    while (!q.empty()) {
      a = q.front();
      q.pop();
      if (a.t.first == a.k.first && a.t.second == a.k.second) {
        ans = a.turn;
        break;
      }
      if (100 <= a.turn + abs(a.t.first - a.k.first) / 2 +
                     abs(a.t.second - a.k.second) / 2) {
        break;
      }
      for (int i = 0; i < 4; i++) {
        b = a;
        b.t.first += dy[i];
        b.t.second += dx[i];
        b.k.first -= dy[i];
        b.k.second -= dx[i];
        if (field[b.t.first][b.t.second])
          b.t = a.t;
        if (field[b.k.first][b.k.second])
          b.k = a.k;
        b.turn = a.turn + 1;
        if (!visited[b.t.first][b.t.second][b.k.first][b.k.second]) {
          visited[b.t.first][b.t.second][b.k.first][b.k.second] = true;
          q.push(b);
        }
      }
    }
    while (!q.empty())
      q.pop();
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

struct State {
  pair<int, int> t, k;
  int turn;
};

int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
int w, h;
int field[52][52];
bool visited[52][52][52][52];

int main() {
  while (cin >> w >> h, w | h) {
    memset(visited, false, sizeof(visited));
    queue<State> q;
    State a, b;
    a.turn = 0;
    cin >> a.t.second >> a.t.first;
    cin >> a.k.second >> a.k.first;
    for (int i = 0; i < 52; i++) {
      for (int j = 0; j < 52; j++)
        field[i][j] = 1;
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++)
        cin >> field[i][j];
    }
    int ans = -1;
    q.push(a);
    visited[a.t.first][a.t.second][a.k.first][a.k.second] = true;
    while (!q.empty()) {
      a = q.front();
      q.pop();
      if (a.t.first == a.k.first && a.t.second == a.k.second) {
        ans = a.turn;
        break;
      }
      if (100 <= a.turn + abs(a.t.first - a.k.first) / 2 +
                     abs(a.t.second - a.k.second) / 2) {
        break;
      }
      for (int i = 0; i < 4; i++) {
        b = a;
        b.t.first += dy[i];
        b.t.second += dx[i];
        b.k.first -= dy[i];
        b.k.second -= dx[i];
        if (field[b.t.first][b.t.second])
          b.t = a.t;
        if (field[b.k.first][b.k.second])
          b.k = a.k;
        b.turn = a.turn + 1;
        if (!visited[b.t.first][b.t.second][b.k.first][b.k.second]) {
          visited[b.t.first][b.t.second][b.k.first][b.k.second] = true;
          q.push(b);
        }
      }
    }
    while (!q.empty())
      q.pop();
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}