Problem p00223 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef pair<int, int> pii;
typedef pair<pii, pii> piiii;

int X, Y;
int ba[50][50];

int dtx[] = {0, 1, 0, -1};
int dty[] = {-1, 0, 1, 0};
int dkx[] = {0, -1, 0, 1};
int dky[] = {1, 0, -1, 0};

int solve(pii T, pii K) {
  map<piiii, bool> visited;
  queue<pair<piiii, int>> Q;
  Q.push(make_pair(piiii(T, K), 0));
  while (Q.size()) {
    pii tt = Q.front().first.first, kk = Q.front().first.second;
    int m = Q.front().second;
    Q.pop();
    if (visited[piiii(tt, kk)])
      continue;
    if (m >= 100)
      break;
    if (tt == kk)
      return m;
    visited[piiii(tt, kk)] = true;
    //    cout << tt.first << ","<<tt.second<<" " << kk.first<<","<<kk.second <<
    //    endl;
    REP(k, 4) {
      int txx = tt.first + dtx[k];
      int tyy = tt.second + dty[k];
      int kxx = kk.first + dkx[k];
      int kyy = kk.second + dky[k];

      if (txx < 0 || X <= txx || tyy < 0 || Y <= tyy || ba[txx][tyy]) {
        txx -= dtx[k];
        tyy -= dty[k];
      }
      if (kxx < 0 || X <= kxx || kyy < 0 || Y <= kyy || ba[kxx][kyy]) {
        kxx -= dkx[k];
        kyy -= dky[k];
      }
      if (!visited[piiii(pii(txx, tyy), pii(kxx, kyy))])
        Q.push(make_pair(piiii(pii(txx, tyy), pii(kxx, kyy)), m + 1));
    }
  }
  return -1;
}

int main() {
  while (cin >> X >> Y, X || Y) {
    int tx, ty, kx, ky;
    cin >> tx >> ty;
    cin >> kx >> ky;
    tx--;
    ty--;
    kx--;
    ky--;
    REP(y, Y)
    REP(x, X)
    cin >> ba[x][y];
    int res = solve(pii(tx, ty), pii(kx, ky));
    if (res < 0)
      cout << "NA" << endl;
    else
      cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef pair<int, int> pii;
typedef pair<pii, pii> piiii;

int X, Y;
int ba[50][50];

int dtx[] = {0, 1, 0, -1};
int dty[] = {-1, 0, 1, 0};
int dkx[] = {0, -1, 0, 1};
int dky[] = {1, 0, -1, 0};

map<piiii, bool> visited;

int solve(pii T, pii K) {
  queue<pair<piiii, int>> Q;
  Q.push(make_pair(piiii(T, K), 0));
  while (Q.size()) {
    pii tt = Q.front().first.first, kk = Q.front().first.second;
    int m = Q.front().second;
    Q.pop();
    if (m >= 100)
      break;
    if (tt == kk)
      return m;
    visited[piiii(tt, kk)] = true;
    //    cout << tt.first << ","<<tt.second<<" " << kk.first<<","<<kk.second <<
    //    endl;
    REP(k, 4) {
      int txx = tt.first + dtx[k];
      int tyy = tt.second + dty[k];
      int kxx = kk.first + dkx[k];
      int kyy = kk.second + dky[k];

      if (txx < 0 || X <= txx || tyy < 0 || Y <= tyy || ba[txx][tyy]) {
        txx -= dtx[k];
        tyy -= dty[k];
      }
      if (kxx < 0 || X <= kxx || kyy < 0 || Y <= kyy || ba[kxx][kyy]) {
        kxx -= dkx[k];
        kyy -= dky[k];
      }
      if (!visited[piiii(pii(txx, tyy), pii(kxx, kyy))])
        Q.push(make_pair(piiii(pii(txx, tyy), pii(kxx, kyy)), m + 1));
    }
  }
  return -1;
}

int main() {
  while (cin >> X >> Y, X || Y) {
    int tx, ty, kx, ky;
    cin >> tx >> ty;
    cin >> kx >> ky;
    tx--;
    ty--;
    kx--;
    ky--;
    REP(y, Y)
    REP(x, X)
    cin >> ba[x][y];
    int res = solve(pii(tx, ty), pii(kx, ky));
    if (res < 0)
      cout << "NA" << endl;
    else
      cout << res << endl;
  }
}