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

#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
struct Y {
  int hx, hy, rx, ry, t;
};
int c[51][51][51][51], mp[52][52];
int main() {
  int w, h, i, j, a[2] = {1, -1}, b[2];
  Y e;
  while (1) {
    queue<Y> q;
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    cin >> e.hx >> e.hy >> e.rx >> e.ry;
    e.t = 0;
    q.push(e);
    memset(c, 0, sizeof(c));
    memset(mp, 1, sizeof(mp));
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        cin >> mp[i][j];
      }
    }
    while (!q.empty()) {
      e = q.front();
      q.pop();
      c[e.hy][e.hx][e.ry][e.rx] = 1;
      if (e.t >= 100 || e.hx == e.rx && e.hy == e.ry)
        break;
      e.t++;
      for (i = 0; i < 2; i++) {
        if (mp[e.hy + a[i]][e.hx] == 0 &&
                c[e.hy + a[i]][e.hx][e.ry - a[i]][e.rx] == 0 &&
                mp[e.ry - a[i]][e.rx] == 0 ||
            mp[e.ry - a[i]][e.rx] == 0 &&
                c[e.hy][e.hx][e.ry - a[i]][e.rx] == 0 &&
                mp[e.hy + a[i]][e.hx] != 0 ||
            mp[e.hy + a[i]][e.hx] == 0 && mp[e.ry - a[i]][e.rx] != 0 &&
                c[e.hy + a[i]][e.hx][e.ry][e.rx] == 0) {
          b[0] = 0;
          b[1] = 0;
          if (mp[e.hy + a[i]][e.hx] == 0) {
            e.hy += a[i];
            b[0] = 1;
          }
          if (mp[e.ry - a[i]][e.rx] == 0) {
            e.ry -= a[i];
            b[1] = 1;
          }
          c[e.hy][e.hx][e.ry][e.rx] = 1;
          q.push(e);
          if (b[0] == 1)
            e.hy -= a[i];
          if (b[1] == 1)
            e.ry += a[i];
        }
        if (mp[e.hy][e.hx + a[i]] == 0 && mp[e.ry][e.rx - a[i]] == 0 &&
                c[e.hy][e.hx + a[i]][e.ry][e.rx - a[i]] == 0 ||
            mp[e.hy][e.hx + a[i]] == 0 &&
                c[e.hy][e.hx + a[i]][e.ry][e.rx] == 0 &&
                mp[e.ry][e.rx - a[i]] != 0 ||
            mp[e.ry][e.rx - a[i]] == 0 && mp[e.hy][e.hx + a[i]] != 0 &&
                c[e.hy][e.hx][e.ry][e.rx - a[i]] == 0) {
          b[0] = 0;
          b[1] = 0;
          if (mp[e.hy][e.hx + a[i]] == 0) {
            e.hx += a[i];
            b[0] = 1;
          }
          if (mp[e.ry][e.rx - a[i]] == 0) {
            e.rx -= a[i];
            b[1] = 1;
          }
          c[e.hy][e.hx][e.ry][e.rx] = 1;
          q.push(e);
          if (b[0] == 1)
            e.hx -= a[i];
          if (b[1] == 1)
            e.rx += a[i];
        }
      }
      e.t--;
    }
    if (e.hy == e.ry && e.hx == e.rx)
      cout << e.t << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
struct Y {
  int hx, hy, rx, ry, tim;
};
int c[51][51][51][51];
int main() {
  int n, w, h, i, j, a[2] = {1, -1}, b[2];
  Y e;
  while (cin >> w >> h && w != 0 && h != 0) {
    int mp[52][52] = {0};
    queue<Y> q;
    cin >> e.hx >> e.hy >> e.rx >> e.ry;
    e.tim = 0;
    q.push(e);
    memset(c, 0, sizeof(c));
    for (i = 0; i <= h + 1; i++) {
      mp[i][0] = 1;
      mp[i][w + 1] = 1;
    }
    for (i = 0; i <= w + 1; i++) {
      mp[0][i] = 1;
      mp[h + 1][i] = 1;
    }
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        cin >> mp[j][i];
      }
    }
    while (!q.empty()) {
      e = q.front();
      q.pop();
      c[e.hy][e.hx][e.ry][e.rx] = 1;
      if (e.tim >= 100 || e.hx == e.rx && e.hy == e.ry)
        break;
      e.tim++;
      for (i = 0; i < 2; i++) {
        if (mp[e.hy + a[i]][e.hx] != 1 &&
                c[e.hy + a[i]][e.hx][e.ry - a[i]][e.rx] == 0 &&
                mp[e.ry - a[i]][e.rx] != 1 ||
            mp[e.ry - a[i]][e.rx] != 1 &&
                c[e.hy][e.hx][e.ry - a[i]][e.rx] == 0 &&
                mp[e.hy + a[i]][e.hx] == 1 ||
            mp[e.hy + a[i]][e.hx] != 1 && mp[e.ry - a[i]][e.rx] == 1 &&
                c[e.hy + a[i]][e.hx][e.ry][e.rx] == 0) {
          b[0] = 0;
          b[1] = 0;
          if (mp[e.hy + a[i]][e.hx] != 1) {
            e.hy += a[i];
            b[0] = 1;
          }
          if (mp[e.ry - a[i]][e.rx] != 1) {
            e.ry -= a[i];
            b[1] = 1;
          }
          c[e.hy][e.hx][e.ry][e.rx] = 1;
          q.push(e);
          if (b[0] == 1)
            e.hy -= a[i];
          if (b[1] == 1)
            e.ry += a[i];
        }
        if (mp[e.hy][e.hx + a[i]] != 1 && mp[e.ry][e.rx - a[i]] != 1 &&
                c[e.hy][e.hx + a[i]][e.ry][e.rx - a[i]] == 0 ||
            mp[e.hy][e.hx + a[i]] != 1 &&
                c[e.hy][e.hx + a[i]][e.ry][e.rx] == 0 &&
                mp[e.ry][e.rx - a[i]] == 1 ||
            mp[e.ry][e.rx - a[i]] != 1 && mp[e.hy][e.hx + a[i]] == 1 &&
                c[e.hy][e.hx][e.ry][e.rx - a[i]] == 0) {
          b[0] = 0;
          b[1] = 0;
          if (mp[e.hy][e.hx + a[i]] != 1) {
            e.hx += a[i];
            b[0] = 1;
          }
          if (mp[e.ry][e.rx - a[i]] != 1) {
            e.rx -= a[i];
            b[1] = 1;
          }
          c[e.hy][e.hx][e.ry][e.rx] = 1;
          q.push(e);
          if (b[0] == 1)
            e.hx -= a[i];
          if (b[1] == 1)
            e.rx += a[i];
        }
      }
      e.tim--;
    }
    if (e.hy == e.ry && e.hx == e.rx)
      cout << e.tim << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}