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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#define fr first
#define sc second
#define INF (2 << 28)
using namespace std;

typedef pair<int, int> iP;
typedef pair<iP, iP> iiP;
typedef pair<iiP, int> iiiP;

int w, h;
iP ts, ks;
int mas[51][51], used[51][51][51][51];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int bfs() {

  fill_n(***used, 51 * 51 * 51 * 51, INF);
  queue<iiiP> que;
  que.push(iiiP(iiP(ts, ks), 0));
  used[ts.fr][ts.sc][ks.fr][ks.sc] = 0;
  while (!que.empty()) {
    iiiP p = que.front();
    que.pop();
    if (p.fr.fr == p.fr.sc) {
      if (p.sc >= 100)
        return -1;
      else
        return p.sc;
    }

    int tx = p.fr.fr.fr, ty = p.fr.fr.sc, kx = p.fr.sc.fr, ky = p.fr.sc.sc;
    if (used[tx][ty][kx][ky] == INF)
      continue;
    for (int i = 0; i < 4; i++) {
      int ntx = tx + dx[i], nty = ty + dy[i];
      int nkx = kx - dx[i], nky = ky - dy[i];

      if (mas[nty][ntx] == 1 || !(ntx >= 0 && ntx < w && nty >= 0 && nty < h))
        ntx = tx, nty = ty;
      if (mas[nky][nkx] == 1 || !(nkx >= 0 && nkx < w && nky >= 0 && nky < h))
        nkx = kx, nky = ky;

      if (used[ntx][nty][nkx][nky] > p.sc + 1) {
        que.push(iiiP(iiP(iP(ntx, nty), iP(nkx, nky)), p.sc + 1));
        used[ntx][nty][nkx][nky] = p.sc + 1;
      }
    }
  }
  return -1;
}

int main() {

  while (cin >> w >> h, w || h) {
    cin >> ts.fr >> ts.sc;
    cin >> ks.fr >> ks.sc;
    ts.fr--, ts.sc--;
    ks.fr--, ks.sc--;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++)
        cin >> mas[i][j];
    }

    int ans = bfs();
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#define fr first
#define sc second
using namespace std;

typedef pair<int, int> iP;
typedef pair<iP, iP> iiP;
typedef pair<iiP, int> iiiP;

int w, h;
iP ts, ks;
int mas[51][51], used[51][51][51][51];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int bfs() {

  memset(used, -1, sizeof(used));
  queue<iiiP> que;
  que.push(iiiP(iiP(ts, ks), 0));
  used[ts.fr][ts.sc][ks.fr][ks.sc] = 0;
  while (!que.empty()) {
    iiiP p = que.front();
    que.pop();
    if (p.fr.fr == p.fr.sc) {
      if (p.sc >= 100)
        return -1;
      else
        return p.sc;
    }
    int tx = p.fr.fr.fr, ty = p.fr.fr.sc, kx = p.fr.sc.fr, ky = p.fr.sc.sc;
    for (int i = 0; i < 4; i++) {
      int ntx = tx + dx[i], nty = ty + dy[i];
      int nkx = kx - dx[i], nky = ky - dy[i];
      if (~used[ntx][nty][nkx][nky])
        continue;
      if (ntx >= 0 && ntx < w && nty >= 0 && nty < h && nkx >= 0 && nkx < w &&
          nky >= 0 && nky < h &&
          used[ntx][nty][nkx][nky] < used[tx][ty][kx][ky] + 1) {
        que.push(iiiP(iiP(iP(ntx, nty), iP(nkx, nky)), p.sc + 1));
        used[ntx][nty][nkx][nky] = used[tx][ty][kx][ky] + 1;
      }
    }
  }
  return -1;
}

int main() {

  while (cin >> w >> h, w || h) {
    cin >> ts.fr >> ts.sc;
    cin >> ks.fr >> ks.sc;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++)
        cin >> mas[i][j];
    }

    int ans = bfs();
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}