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

#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;

struct AO {
  int tx;
  int ty;
  int kx;
  int ky;
  int cnt;
};

queue<AO> Q;
AO TK, f;
int d[52][52];
int sa[51][51][51][51];

int main() {
  int X, Y, F;                    //     tx ty kx ky
  int mv[4][4] = {{0, -1, 0, 1},  //???
                  {0, 1, 0, -1},  //???
                  {-1, 0, 1, 0},  //???
                  {1, 0, -1, 0}}; //???
  while (1) {
    F = 1;
    for (int i = 0; i < 52; i++) {
      for (int j = 0; j < 52; j++) {
        d[i][j] = 1;
      }
    }
    memset(sa, 0, sizeof(sa));
    // printf("%d\\n",d[0][0]);
    scanf("%d %d", &X, &Y);
    if (X == 0 && Y == 0)
      break;
    scanf("%d %d %d %d", &TK.tx, &TK.ty, &TK.kx, &TK.ky);
    sa[TK.ty][TK.tx][TK.ky][TK.kx] = 1;
    TK.cnt = 0;
    Q.push(TK);
    for (int i = 1; i <= Y; i++) {
      for (int j = 1; j <= X; j++) {
        scanf("%d", &d[i][j]);
      }
    }

    while (!Q.empty()) { //! Q.empty()
      TK = Q.front();
      Q.pop();
      // printf("?????? ??????:%d %d  ??????:%d %d\\n",TK.tx,TK.ty,TK.kx,TK.ky);
      if (TK.cnt >= 100) {
        break;
      }
      if (TK.tx == TK.kx && TK.ty == TK.ky) {
        F = 0;
        break;
      }
      if (TK.cnt >= 100) {
        break;
      }
      for (int i = 0; i < 4; i++) {
        f.tx = TK.tx + mv[i][0];
        f.ty = TK.ty + mv[i][1];
        f.kx = TK.kx + mv[i][2];
        f.ky = TK.ky + mv[i][3];
        f.cnt = TK.cnt + 1;
        // printf("A");
        if (d[f.ty][f.tx] == 0 || d[f.ky][f.kx] == 0) {
          if (d[f.ty][f.tx] == 1) {
            f.tx = TK.tx;
            f.ty = TK.ty;
          }
          if (d[f.ky][f.kx] == 1) {
            f.kx = TK.kx;
            f.ky = TK.ky;
          }
        } else if (d[f.ty][f.tx] == 1 && d[f.ky][f.kx] == 1) {
          continue;
        }
        if (sa[f.ty][f.tx][f.ky][f.kx] == 1 || f.tx < 0 || f.kx < 0 ||
            f.ty < 0 || f.ky < 0 || f.tx > X || f.kx > X || f.ty > Y ||
            f.ky > Y) {
          continue;
        }
        sa[f.ty][f.tx][f.ky][f.kx] = 1;
        Q.push(f);
        // printf("??????%d %d\\n?????? %d %d\\n",f.tx,f.ty,f.kx,f.ky);
      }
      // printf("------------\\n");
    }
    if (F == 0)
      printf("%d\\n", TK.cnt);
    // printf("%d %d %d %d\\n",TK.tx,TK.ty,TK.kx,TK.ky);
    else
      printf("NA\\n");
    while (!Q.empty())
      Q.pop();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;

struct AO {
  int tx;
  int ty;
  int kx;
  int ky;
  int cnt;
};

queue<AO> Q;
AO TK, f;
int d[51][51];
int sa[51][51][51][51];

int main() {
  int X, Y, F;                    //     tx ty kx ky
  int mv[4][4] = {{0, -1, 0, 1},  //???
                  {0, 1, 0, -1},  //???
                  {-1, 0, 1, 0},  //???
                  {1, 0, -1, 0}}; //???
  while (1) {
    F = 1;
    for (int i = 0; i < 52; i++) {
      for (int j = 0; j < 52; j++) {
        d[i][j] = 1;
      }
    }
    memset(sa, 0, sizeof(sa));
    // printf("%d\\n",d[0][0]);
    scanf("%d %d", &X, &Y);
    if (X == 0 && Y == 0)
      break;
    scanf("%d %d %d %d", &TK.tx, &TK.ty, &TK.kx, &TK.ky);
    sa[TK.ty][TK.tx][TK.ky][TK.kx] = 1;
    TK.cnt = 0;
    Q.push(TK);
    for (int i = 1; i <= Y; i++) {
      for (int j = 1; j <= X; j++) {
        scanf("%d", &d[i][j]);
      }
    }

    while (!Q.empty()) { //! Q.empty()
      TK = Q.front();
      Q.pop();
      // printf("?????? ??????:%d %d  ??????:%d %d\\n",TK.tx,TK.ty,TK.kx,TK.ky);
      if (TK.cnt >= 100) {
        break;
      }
      if (TK.tx == TK.kx && TK.ty == TK.ky) {
        F = 0;
        break;
      }
      if (TK.cnt >= 100) {
        break;
      }
      for (int i = 0; i < 4; i++) {
        f.tx = TK.tx + mv[i][0];
        f.ty = TK.ty + mv[i][1];
        f.kx = TK.kx + mv[i][2];
        f.ky = TK.ky + mv[i][3];
        f.cnt = TK.cnt + 1;
        // printf("A");
        if (d[f.ty][f.tx] == 0 || d[f.ky][f.kx] == 0) {
          if (d[f.ty][f.tx] == 1) {
            f.tx = TK.tx;
            f.ty = TK.ty;
          }
          if (d[f.ky][f.kx] == 1) {
            f.kx = TK.kx;
            f.ky = TK.ky;
          }
        } else if (d[f.ty][f.tx] == 1 && d[f.ky][f.kx] == 1) {
          continue;
        }
        if (sa[f.ty][f.tx][f.ky][f.kx] == 1 || f.tx < 0 || f.kx < 0 ||
            f.ty < 0 || f.ky < 0 || f.tx > X || f.kx > X || f.ty > Y ||
            f.ky > Y) {
          continue;
        }
        sa[f.ty][f.tx][f.ky][f.kx] = 1;
        Q.push(f);
        // printf("??????%d %d\\n?????? %d %d\\n",f.tx,f.ty,f.kx,f.ky);
      }
      // printf("------------\\n");
    }
    if (F == 0)
      printf("%d\\n", TK.cnt);
    // printf("%d %d %d %d\\n",TK.tx,TK.ty,TK.kx,TK.ky);
    else
      printf("NA\\n");
    while (!Q.empty())
      Q.pop();
  }
  return 0;
}