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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
int dp[55][55][55][55];
bool u[55][55][55][55];
int main() {
  while (1) {
    int x, y;
    int ax, ay;
    int bx, by;
    cin >> x >> y;
    if (x == 0)
      return 0;
    cin >> ax >> ay;
    ax--;
    ay--;
    cin >> bx >> by;
    bx--;
    by--;
    int f[55][55];
    for (int i = 0; i < y; i++) {
      for (int j = 0; j < x; j++) {
        cin >> f[i][j];
      }
    }
    for (int i = 0; i < 55; i++)
      for (int j = 0; j < 55; j++)
        for (int k = 0; k < 55; k++)
          for (int l = 0; l < 55; l++)
            dp[i][j][k][l] = INF, u[i][j][k][l] = 0;
    dp[ay][ax][by][bx] = 0;
    queue<P2> que;
    que.push(mp(mp(ay, ax), mp(by, bx)));
    while (!que.empty()) {
      P2 p = que.front();
      que.pop();
      if (u[p.fi.fi][p.fi.sc][p.sc.fi][p.sc.sc])
        continue;
      u[p.fi.fi][p.fi.sc][p.sc.fi][p.sc.sc] = true;
      int dx[4] = {0, 1, 0, -1};
      int dy[4] = {1, 0, -1, 0};
      for (int k = 0; k < 4; k++) {
        int ny = p.fi.fi + dy[k];
        int nx = p.fi.sc + dx[k];
        if (0 <= ny && ny < y && 0 <= nx && nx < x && f[ny][nx] == 0) {
        } else {
          ny = p.fi.fi;
          nx = p.fi.sc;
        }
        int ny2 = p.sc.fi + dy[(k + 2) % 4];
        int nx2 = p.sc.sc + dx[(k + 2) % 4];
        if (0 <= ny2 && ny2 < y && 0 <= nx2 && nx2 < x && f[ny2][nx2] == 0) {
        } else {
          ny2 = p.sc.fi;
          nx2 = p.sc.sc;
        }
        if (dp[ny][nx][ny2][nx2] > dp[p.fi.fi][p.fi.sc][p.sc.fi][p.sc.sc] + 1) {
          dp[ny][nx][ny2][nx2] = dp[p.fi.fi][p.fi.sc][p.sc.fi][p.sc.sc] + 1;
          que.push(mp(mp(ny, nx), mp(ny2, nx2)));
        }
      }
    }
    int res = INF;
    for (int i = 0; i < y; i++)
      for (int j = 0; j < x; j++)
        res = min(res, dp[i][j][i][j]);
    if (res >= 100)
      puts("NA");
    else
      printf("%d\\n", res);
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
int main() {
  while (1) {
    int x, y;
    int ax, ay;
    int bx, by;
    cin >> x >> y;
    if (x == 0)
      return 0;
    cin >> ax >> ay;
    cin >> bx >> by;
    int f[55][55];
    for (int i = 0; i < y; i++) {
      for (int j = 0; j < x; j++) {
        cin >> f[i][j];
      }
    }
    int dp[55][55][55][55];
    for (int i = 0; i < 55; i++)
      for (int j = 0; j < 55; j++)
        for (int k = 0; k < 55; k++)
          for (int l = 0; l < 55; l++)
            dp[i][j][k][l] = INF;
    dp[ay][ax][by][bx] = 0;
    queue<P2> que;
    que.push(mp(mp(ay, ax), mp(by, bx)));
    while (!que.empty()) {
      P2 p = que.front();
      que.pop();
      int dx[4] = {0, 1, 0, -1};
      int dy[4] = {1, 0, -1, 0};
      for (int k = 0; k < 4; k++) {
        int ny = p.fi.fi + dy[k];
        int nx = p.fi.sc + dx[k];
        if (0 <= ny && ny < y && 0 <= nx && nx < x && f[ny][nx] == 0) {
        } else {
          ny = p.fi.fi;
          nx = p.fi.sc;
        }
        int ny2 = p.sc.fi + dy[(k + 2) % 4];
        int nx2 = p.sc.sc + dx[(k + 2) % 4];
        if (0 <= ny2 && ny2 < y && 0 <= nx2 && nx2 < x && f[ny2][nx2] == 0) {
        } else {
          ny2 = p.sc.fi;
          nx2 = p.sc.sc;
        }
        if (dp[ny][nx][ny2][nx2] > dp[p.fi.fi][p.fi.sc][p.sc.fi][p.sc.sc] + 1) {
          dp[ny][nx][ny2][nx2] = dp[p.fi.fi][p.fi.sc][p.sc.fi][p.sc.sc] + 1;
          que.push(mp(mp(ny, nx), mp(ny2, nx2)));
        }
      }
    }
    int res = INF;
    for (int i = 0; i < y; i++)
      for (int j = 0; j < x; j++)
        res = min(res, dp[i][j][i][j]);
    if (res == INF)
      puts("NA");
    else
      printf("%d\\n", res);
  }
}