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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int x, y;
int tm[51][51][51][51];
char field[51][51];
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};
struct twins {
  int x1;
  int y1;
  int x2;
  int y2;
};

bool can_move(int x1, int y1) {
  if (x1 > 0 && x1 <= x && y1 > 0 && y1 <= y) {
    if (field[y1][x1] == '0')
      return true;
    else
      return false;
  }
  return false;
}

void get_time(int x1, int y1, int x2, int y2) {
  twins t0;
  t0.x1 = x1, t0.x2 = x2, t0.y1 = y1, t0.y2 = y2;
  queue<twins> qt;
  qt.push(t0);
  while (!qt.empty()) {
    twins t = qt.front();
    qt.pop();
    for (int i = 0; i < 4; i++) {
      int ay = t.y1 + dy[i];
      int ax = t.x1 + dx[i];
      int by = t.y2 - dy[i];
      int bx = t.x2 - dx[i];
      if (!can_move(ax, ay)) {
        ay = t.y1;
        ax = t.x1;
      }
      if (!can_move(bx, by)) {
        by = t.y2;
        bx = t.x2;
      }
      if (tm[ay][ax][by][bx] < 0) {
        tm[ay][ax][by][bx] = tm[t.y1][t.x1][t.y2][t.x2] + 1;
        t0.x1 = ax, t0.x2 = bx, t0.y1 = ay, t0.y2 = by;
        qt.push(t0);
      }
    }
  }
}

int main() {
  int ax, ay, bx, by;
  while (cin >> x >> y && (x || y)) {
    int count = 101;
    memset(tm, -1, sizeof(tm));
    cin >> ax >> ay >> bx >> by;
    for (int i = 1; i <= y; i++) {
      for (int j = 1; j <= x; j++) {
        cin >> field[i][j];
      }
    }
    tm[ay][ax][by][bx] = 0;
    get_time(ax, ay, bx, by);
    for (int i = 1; i <= y; i++) {
      for (int j = 1; j <= x; j++) {
        if (tm[i][j][i][j] >= 0)
          count = min(count, tm[i][j][i][j]);
      }
    }
    if (count == 101)
      cout << "NA" << endl;
    else
      cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int x, y;
int tm[51][51][51][51];
char field[51][51];
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};
struct twins {
  int x1;
  int y1;
  int x2;
  int y2;
};

bool can_move(int x1, int y1) {
  if (x1 > 0 && x1 <= x && y1 > 0 && y1 <= y) {
    if (field[y1][x1] == '0')
      return true;
    else
      return false;
  }
  return false;
}

void get_time(int x1, int y1, int x2, int y2) {
  twins t0;
  t0.x1 = x1, t0.x2 = x2, t0.y1 = y1, t0.y2 = y2;
  queue<twins> qt;
  qt.push(t0);
  while (!qt.empty()) {
    twins t = qt.front();
    qt.pop();
    for (int i = 0; i < 4; i++) {
      int ay = t.y1 + dy[i];
      int ax = t.x1 + dx[i];
      int by = t.y2 - dy[i];
      int bx = t.x2 - dx[i];
      if (!can_move(ax, ay)) {
        ay = t.y1;
        ax = t.x1;
      }
      if (!can_move(bx, by)) {
        by = t.y2;
        bx = t.x2;
      }
      if (tm[ay][ax][by][bx] < 0 ||
          tm[ay][ax][by][bx] > tm[t.y1][t.x1][t.y2][t.x2] + 1) {
        tm[ay][ax][by][bx] = tm[t.y1][t.x1][t.y2][t.x2] + 1;
        t0.x1 = ax, t0.x2 = bx, t0.y1 = ay, t0.y2 = by;
        qt.push(t0);
      }
    }
  }
}

int main() {
  int ax, ay, bx, by;
  while (cin >> x >> y && (x || y)) {
    int count = 101;
    memset(tm, -1, sizeof(tm));
    cin >> ax >> ay >> bx >> by;
    for (int i = 1; i <= y; i++) {
      for (int j = 1; j <= x; j++) {
        cin >> field[i][j];
      }
    }
    tm[ay][ax][by][bx] = 0;
    get_time(ax, ay, bx, by);
    for (int i = 1; i <= y; i++) {
      for (int j = 1; j <= x; j++) {
        if (tm[i][j][i][j] >= 0)
          count = min(count, tm[i][j][i][j]);
      }
    }
    if (count == 101)
      cout << "NA" << endl;
    else
      cout << count << endl;
  }
  return 0;
}