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

#include <algorithm>
#include <cmath>
#include <complex>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)
#define FORE(i, a, b) for (int i = (a); i <= (b); i++)
using namespace std;
typedef complex<int> cl;
typedef pair<cl, cl> pr;
typedef pair<pr, int> ppr;
typedef vector<pr> vpr;
const int INF = 1 << 30;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
bool reached[52][52][52][52] = {};
ppr pprs(cl a, cl b, int c) { return ppr(pr(a, b), c); }
int main() {
  // your code goes here
  int X, Y, tmp, tmpb;
  while (cin >> X >> Y && X) {
    REP(i, 52) REP(j, 52) REP(k, 52) REP(l, 52) reached[i][j][k][l] = false;
    bool maps[52][52] = {};
    cl t, k;
    cin >> tmp >> tmpb;
    t = cl(tmpb, tmp);
    cin >> tmp >> tmpb;
    k = cl(tmpb, tmp);
    REP(i, Y + 2)
    REP(j, X + 2)
    maps[i][j] = 1;
    FORE(i, 1, Y)
    FORE(j, 1, X)
    cin >> maps[i][j];
    queue<ppr> qu;
    qu.push(pprs(t, k, 0));
    int ans = -1;
    while (!qu.empty()) {
      cl a = qu.front().first.first, b = qu.front().first.second;
      int t = qu.front().second;
      qu.pop();
      if (a == b) {
        ans = t;
        break;
      }
      if (reached[a.real()][a.imag()][b.real()][b.imag()])
        continue;
      reached[a.real()][a.imag()][b.real()][b.imag()] = 1;

      REP(i, 4) {

        cl na = cl(a), nb = cl(b);
        if (!maps[a.real() + dy[i]][a.imag() + dx[i]])
          na += cl(dy[i], dx[i]);
        if (!maps[b.real() - dy[i]][b.imag() - dx[i]])
          nb -= cl(dy[i], dx[i]);
        if (!reached[na.real()][na.imag()][nb.real()][nb.imag()])
          qu.push(pprs(na, nb, t + 1));
      }
    }
    if (ans == -1 || ans >= 100)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
int DP[52][52][52][52];
int map[52][52] = {};
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
const int INF = 2 << 20;
int search(int tx, int ty, int kx, int ky, int times) {

  int minim = INF;

  if (tx == kx && ty == ky)
    return 0;
  DP[tx][ty][kx][ky] = INF;
  for (int i = 0; i < 4; i++) {
    int ttx = tx, tty = ty, kkx = kx, kky = ky;
    if (!map[tx + dx[i]][ty + dy[i]]) {
      ttx += dx[i];
      tty += dy[i];
    }
    if (!map[kx - dx[i]][ky - dy[i]]) {
      kkx -= dx[i];
      kky -= dy[i];
    }
    minim = min(minim, search(ttx, tty, kkx, kky, times + 1));
  }
  //	if(tx==3 && ty==3)
  //	cout << tx <<" " <<  ty <<" " <<  kx << " " <<  ky <<  " " << minim <<
  //endl;
  DP[tx][ty][kx][ky] = minim + 1;
  return DP[tx][ty][kx][ky];
}
int main() {
  // your code goes here
  int x, y, tx, ty, kx, ky, ans;
  while (cin >> x >> y && x != 0) {
    for (int i = 0; i < 52; i++)
      for (int j = 0; j < 52; j++)
        for (int k = 0; k < 52; k++)
          for (int l = 0; l < 52; l++)
            DP[i][j][k][l] = -1;
    for (int i = 0; i < 52; i++) {
      map[i][0] = 1;
      map[i][y + 1] = 1;
      map[0][i] = 1;
      map[x + 1][i] = 1;
    }
    cin >> tx >> ty >> kx >> ky;
    for (int i = 1; i <= y; i++)
      for (int j = 1; j <= x; j++)
        cin >> map[j][i];
    ans = search(tx, ty, kx, ky, 0);

    if (ans >= 100)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}