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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, N) for (int I = 0; I < (int)(N); I++)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;

int solve();
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int w, h, tx, ty, kx, ky;
int maps[52][52];
int memo[52][52][52][52];

struct Data {
  P pt, pk;
  int cnt;
  Data(P pt_, P pk_, int cnt_) {
    pt = pt_;
    pk = pk_;
    cnt = cnt_;
  }
  Data() {}
};

int main(void) {
  while (cin >> w >> h, w) {
    cin >> tx >> ty;
    cin >> kx >> ky;
    fill_n(*maps, 52 * 52, 1);
    FOR(i, h) FOR(j, w) { cin >> maps[i + 1][j + 1]; }
    int a = solve();
    if (a == -1 || a >= 100)
      FIN("NA");
    else
      FIN(a);
  }
  return 0;
}

int solve() {
  int ans = -1;
  fill_n(***memo, 52 * 52 * 52 * 52, INF);
  queue<Data> que;
  que.push(Data(P(tx, ty), P(kx, ky), 0));
  while (que.size()) {
    Data data = que.front();
    que.pop();
    int x1 = data.pt.first;
    int y1 = data.pt.second;
    int x2 = data.pk.first;
    int y2 = data.pk.second;
    int cost = data.cnt;
    if (cost > 100)
      break;
    if (memo[x1][y1][x2][y2] <= cost)
      continue;
    memo[x1][y1][x2][y2] = cost;
    if (x1 == x2 && y1 == y2) {
      ans = cost;
      break;
    };
    FOR(i, 4) {
      int nx1 = x1 + dx[i];
      int ny1 = y1 + dy[i];
      int nx2 = x2 - dx[i];
      int ny2 = y2 - dy[i];
      if (maps[ny1][nx1] == 1) {
        nx1 = x1;
        ny1 = y1;
      };
      if (maps[ny2][nx2] == 1) {
        nx2 = x2;
        ny2 = y2;
      };
      que.push(Data(P(nx1, ny1), P(nx2, ny2), cost + 1));
    }
  }

  return ans;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, N) for (int I = 0; I < (int)(N); I++)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;

int solve();
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int w, h, tx, ty, kx, ky;
int maps[51][51];
int memo[51][51][51][51];

struct Data {
  P pt, pk;
  int cnt;
  Data(P pt_, P pk_, int cnt_) {
    pt = pt_;
    pk = pk_;
    cnt = cnt_;
  }
  Data() {}
};

int main(void) {
  while (cin >> w >> h, w) {
    cin >> tx >> ty;
    cin >> kx >> ky;
    fill_n(*maps, 51 * 51, 1);
    FOR(i, h) FOR(j, w) { cin >> maps[i + 1][j + 1]; }
    int a = solve();
    if (a == -1 || a >= 100)
      FIN("NA");
    else
      FIN(a);
  }
  return 0;
}

int solve() {
  int ans = -1;
  fill_n(***memo, 51 * 51 * 51 * 51, INF);
  queue<Data> que;
  que.push(Data(P(tx, ty), P(kx, ky), 0));
  while (que.size()) {
    Data data = que.front();
    que.pop();
    int x1 = data.pt.first;
    int y1 = data.pt.second;
    int x2 = data.pk.first;
    int y2 = data.pk.second;
    int cost = data.cnt;
    if (cost > 100)
      break;
    if (memo[x1][y1][x2][y2] <= cost)
      continue;
    memo[x1][y1][x2][y2] = cost;
    if (x1 == x2 && y1 == y2) {
      ans = cost;
      break;
    };
    FOR(i, 4) {
      int nx1 = x1 + dx[i];
      int ny1 = y1 + dy[i];
      int nx2 = x2 - dx[i];
      int ny2 = y2 - dy[i];
      if (maps[ny1][nx1] == 1) {
        nx1 = x1;
        ny1 = y1;
      };
      if (maps[ny2][nx2] == 1) {
        nx2 = x2;
        ny2 = y2;
      };
      que.push(Data(P(nx1, ny1), P(nx2, ny2), cost + 1));
    }
  }

  return ans;
}