Problem p00223 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define MP make_pair
#define F first
#define S second
using namespace std;
typedef pair<int, int> PII;
typedef pair<PII, PII> PPP;

int x, y;
bool visited[51][51][51][51];
bool flr[52][52];
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int main() {
  while (true) {
    cin >> x >> y;
    if (!x && !y) {
      break;
    }

    int tx, ty, kx, ky;
    memset(visited, false, sizeof visited);
    memset(flr, false, sizeof flr);
    cin >> tx >> ty >> kx >> ky;

    for (int i = 1; i <= y; i++) {
      for (int j = 1; j <= x; j++) {
        int in;
        cin >> in;
        flr[i][j] = (bool)in;
      }
    }

    for (int i = 1; i <= y; i++) {
      flr[i][0] = flr[i][x + 1] = true;
    }
    for (int i = 1; i <= x; i++) {
      flr[0][i] = flr[y + 1][i] = true;
    }

    queue<PPP> q;
    q.push(MP(MP(ty, tx), MP(ky, kx)));

    for (int i = 0; i <= 100; i++) {
      int sz = q.size();
      for (int j = 0; j < sz; j++) {
        PPP now = q.front();
        q.pop();
        if (!visited[now.F.F][now.F.S][now.S.F][now.S.S]) {
          visited[now.F.F][now.F.S][now.S.F][now.S.S] = true;

          if (now.F.F == now.S.F && now.F.S == now.S.S) {
            cout << i << endl;
            goto end;
          }

          for (int k = 0; k < 4; k++) {
            int ntdx = dx[k], ntdy = dy[k], nkdx = -dx[k], nkdy = -dy[k];
            PPP next =
                MP(MP(now.F.F + ntdy * !flr[now.F.F + ntdy][now.F.S + ntdx],
                      now.F.S + ntdx * !flr[now.F.F + ntdy][now.F.S + ntdx]),
                   MP(now.S.F + nkdy * !flr[now.S.F + nkdy][now.S.S + nkdx],
                      now.S.S + nkdx * !flr[now.S.F + nkdy][now.S.S + nkdx]));
            cout << now.F.F + ntdx * !flr[now.F.F + ntdx][now.F.S + ntdy] << " "
                 << now.F.S + ntdy * !flr[now.F.F + ntdx][now.F.S + ntdy] << " "
                 << now.S.F + nkdx * !flr[now.S.F + nkdx][now.S.S + nkdy] << " "
                 << now.S.S + nkdy * !flr[now.S.F + nkdx][now.S.S + nkdy]
                 << endl;
            q.push(next);
          }
        }
      }
    }
    cout << "NA" << endl;
  end:;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MP make_pair
#define F first
#define S second
using namespace std;
typedef pair<int, int> PII;
typedef pair<PII, PII> PPP;

int x, y;
bool visited[51][51][51][51];
bool flr[52][52];
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int main() {
  while (true) {
    cin >> x >> y;
    if (!x && !y) {
      break;
    }

    int tx, ty, kx, ky;
    memset(visited, false, sizeof visited);
    memset(flr, false, sizeof flr);
    cin >> tx >> ty >> kx >> ky;

    for (int i = 1; i <= y; i++) {
      for (int j = 1; j <= x; j++) {
        int in;
        cin >> in;
        flr[i][j] = (bool)in;
      }
    }

    for (int i = 1; i <= y; i++) {
      flr[i][0] = flr[i][x + 1] = true;
    }
    for (int i = 1; i <= x; i++) {
      flr[0][i] = flr[y + 1][i] = true;
    }

    queue<PPP> q;
    q.push(MP(MP(ty, tx), MP(ky, kx)));

    for (int i = 0; i <= 100; i++) {
      int sz = q.size();
      for (int j = 0; j < sz; j++) {
        PPP now = q.front();
        q.pop();
        if (!visited[now.F.F][now.F.S][now.S.F][now.S.S]) {
          visited[now.F.F][now.F.S][now.S.F][now.S.S] = true;

          if (now.F.F == now.S.F && now.F.S == now.S.S) {
            cout << i << endl;
            goto end;
          }

          for (int k = 0; k < 4; k++) {
            int ntdx = dx[k], ntdy = dy[k], nkdx = -dx[k], nkdy = -dy[k];
            PPP next =
                MP(MP(now.F.F + ntdy * !flr[now.F.F + ntdy][now.F.S + ntdx],
                      now.F.S + ntdx * !flr[now.F.F + ntdy][now.F.S + ntdx]),
                   MP(now.S.F + nkdy * !flr[now.S.F + nkdy][now.S.S + nkdx],
                      now.S.S + nkdx * !flr[now.S.F + nkdy][now.S.S + nkdx]));
            q.push(next);
          }
        }
      }
    }
    cout << "NA" << endl;
  end:;
  }
  return 0;
}