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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
struct P {
  int tx, ty;
  int kx, ky;
  int cost;
  P(int _tx, int _ty, int _kx, int _ky, int _cost)
      : tx(_tx), ty(_ty), kx(_kx), ky(_ky), cost(_cost){};
};
int hx, hy, rx, ry;
int X, Y;
int mas[110][110];
bool d[51][51][51][51];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int bfs();
int main() {
  while (1) {
    scanf("%d %d", &X, &Y);
    if (X == 0 && Y == 0)
      break;
    scanf("%d %d", &hx, &hy);
    scanf("%d %d", &rx, &ry);
    for (int i = 1; i <= Y; i++) {
      for (int j = 1; j <= X; j++) {
        scanf("%d", &mas[i][j]);
      }
    }
    int ans = bfs();
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}
int bfs() {
  queue<P> que;
  memset(d, 0, sizeof(d));
  que.push(P(hx, hy, rx, ry, 0));
  d[hx][hy][rx][ry] = 0;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (p.cost >= 100)
      return -1;
    if (p.tx == p.kx && p.ty == p.ky) {
      return p.cost;
    }
    for (int i = 0; i < 4; i++) {
      int nx = p.tx + dx[i], ny = p.ty + dy[i];
      int ux = p.kx - dx[i], uy = p.ky - dy[i];
      if (nx > 0 && ny > 0 && ux > 0 && uy > 0 && nx <= X && ny <= Y &&
          ux <= X && uy <= Y && mas[ny][nx] != 1 && mas[uy][ux] != 1 &&
          d[nx][ny][ux][uy] == 0) {
        que.push(P(nx, ny, ux, uy, p.cost + 1));
        d[nx][ny][ux][uy] = 1;
      } else if ((nx <= 0 || nx > X || ny <= 0 || ny > Y || mas[ny][nx] == 1) &&
                 ux <= X && uy <= Y && ux > 0 && uy > 0 && mas[uy][ux] != 1 &&
                 d[p.tx][p.ty][ux][uy] == 0) {
        que.push(P(p.tx, p.ty, ux, uy, p.cost + 1));
        d[p.tx][p.ty][ux][uy] = 1;
      } else if ((ux <= 0 || ux > X || uy <= 0 || uy > Y || mas[uy][ux] == 1) &&
                 nx <= X && ny <= Y && nx > 0 && ny > 0 && mas[ny][nx] != 1 &&
                 d[nx][ny][p.kx][p.ky] == 0) {
        que.push(P(nx, ny, p.kx, p.ky, p.cost + 1));
        d[nx][ny][p.kx][p.ky] = 1;
      }
    }
  }
  return -1;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
using namespace std;
struct P {
  int tx, ty;
  int kx, ky;
  int cost;
  P(int _tx, int _ty, int _kx, int _ky, int _cost)
      : tx(_tx), ty(_ty), kx(_kx), ky(_ky), cost(_cost){};
};
int hx, hy, rx, ry;
int X, Y;
int mas[110][110];
bool d[51][51][51][51];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int bfs();
int main() {
  while (1) {
    scanf("%d %d", &X, &Y);
    if (X == 0 && Y == 0)
      break;
    scanf("%d %d", &hx, &hy);
    scanf("%d %d", &rx, &ry);
    for (int i = 1; i <= Y; i++) {
      for (int j = 1; j <= X; j++) {
        scanf("%d", &mas[i][j]);
      }
    }
    int ans = bfs();
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}
int bfs() {
  queue<P> que;
  memset(d, 0, sizeof(d));
  que.push(P(hx, hy, rx, ry, 0));
  d[hx][hy][rx][ry] = 0;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (p.cost >= 100)
      return -1;
    if (p.tx == p.kx && p.ty == p.ky) {
      return p.cost;
    }
    for (int i = 0; i < 4; i++) {
      int nx = p.tx + dx[i], ny = p.ty + dy[i];
      int ux = p.kx - dx[i], uy = p.ky - dy[i];
      if (nx > 0 && ny > 0 && ux > 0 && uy > 0 && nx <= X && ny <= Y &&
          ux <= X && uy <= Y && mas[ny][nx] != 1 && mas[uy][ux] != 1 &&
          d[nx][ny][ux][uy] == 0) {
        que.push(P(nx, ny, ux, uy, p.cost++));
        d[nx][ny][ux][uy] = 1;
      } else if ((nx <= 0 || nx > X || ny <= 0 || ny > Y || mas[ny][nx] == 1) &&
                 ux <= X && uy <= Y && ux > 0 && uy > 0 && mas[uy][ux] != 1 &&
                 d[nx][ny][ux][uy] == 0) {
        que.push(P(p.tx, p.ty, ux, uy, p.cost++));
        d[p.tx][p.ty][ux][uy] = 1;
      } else if ((ux <= 0 || ux > X || uy <= 0 || uy > Y || mas[uy][ux] == 1) &&
                 nx <= X && ny <= Y && nx > 0 && ny > 0 && mas[ny][nx] != 1 &&
                 d[nx][ny][ux][uy] == 0) {
        que.push(P(nx, ny, p.kx, p.ky, p.cost++));
        d[nx][ny][p.kx][p.ky] = 1;
      }
    }
  }
  return -1;
}