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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define endl "\\n"
#define INT long long int
using namespace std;
char C[1010][1010];
int A[1010][1010];
int AA[4] = {0, 0, 1, -1};
int AB[4] = {1, -1, 0, 0};
int BA[20] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
              0,  1,  1,  1,  1,  2,  2,  2,  2,  2};
int BB[20] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
              2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};
int h, w;
vector<pair<int, int>> V[2];
void dfs(int c1, int c2, int k) {
  int i;
  A[c1][c2] = k;
  V[k % 2].push_back({c1, c2});
  for (i = 0; i < 4; i++) {
    if (c1 + AA[i] >= 0 and c1 + AA[i] < h and c2 + AB[i] >= 0 and
        c2 + AB[i] < w and A[c1 + AA[i]][c2 + AB[i]] == 0 and
        C[c1 + AA[i]][c2 + AB[i]] == '.') {
      dfs(c1 + AA[i], c2 + AB[i], k);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  int i, c1, c2, d1, d2, j, k, ii, aa, bb;
  cin >> h >> w >> c1 >> c2 >> d1 >> d2;
  for (i = 0; i < h; i++)
    cin >> C[i];
  dfs(c1 - 1, c2 - 1, 1);
  for (i = 1;; i++) {
    if (A[d1 - 1][d2 - 1]) {
      cout << A[d1 - 1][d2 - 1] - 1 << endl;
      return 0;
    }
    if (V[i % 2].size() == 0)
      break;
    V[(i + 1) % 2].clear();
    for (j = 0; j < V[i % 2].size(); j++) {
      aa = V[i % 2][j].first;
      bb = V[i % 2][j].second;
      for (ii = 0; ii < 20; ii++) {
        if (aa + BA[ii] >= 0 and aa + BA[ii] < h and bb + BB[ii] >= 0 and
            bb + BB[ii] < w and A[aa + BA[ii]][bb + BB[ii]] == 0 and
            C[aa + BA[ii]][bb + BB[ii]] == '.') {
          dfs(aa + BA[ii], bb + BB[ii], i + 1);
        }
      }
    }
  }
  cout << -1 << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define endl "\\n"
#define INT long long int
using namespace std;
char C[1010][1010];
int A[1010][1010];
int AA[4] = {0, 0, 1, -1};
int AB[4] = {1, -1, 0, 0};
int BA[20] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
              0,  1,  1,  1,  1,  2,  2,  2,  2,  2};
int BB[20] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
              2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};
int h, w;
void dfs(int c1, int c2, int k) {
  int i;
  A[c1][c2] = k;
  for (i = 0; i < 4; i++) {
    if (c1 + AA[i] >= 0 and c1 + AA[i] < h and c2 + AB[i] >= 0 and
        c2 + AB[i] < w and A[c1 + AA[i]][c2 + AB[i]] == 0 and
        C[c1 + AA[i]][c2 + AB[i]] == '.') {
      dfs(c1 + AA[i], c2 + AB[i], k);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  int i, c1, c2, d1, d2, j, k, ii, t = 0;
  cin >> h >> w >> c1 >> c2 >> d1 >> d2;
  for (i = 0; i < h; i++)
    cin >> C[i];
  dfs(c1 - 1, c2 - 1, 1);
  for (i = 1;; i++) {
    if (A[d1 - 1][d2 - 1]) {
      cout << A[d1 - 1][d2 - 1] - 1 << endl;
      return 0;
    }
    if (t)
      break;
    t = 1;
    for (j = 0; j < h; j++) {
      for (k = 0; k < w; k++) {
        if (A[j][k] == i) {
          for (ii = 0; ii < 20; ii++) {
            if (j + BA[ii] >= 0 and j + BA[ii] < h and k + BB[ii] >= 0 and
                k + BB[ii] < w and A[j + BA[ii]][k + BB[ii]] == 0 and
                C[j + BA[ii]][k + BB[ii]] == '.') {
              dfs(j + BA[ii], k + BB[ii], i + 1);
            }
          }
          t = 0;
        }
      }
    }
  }
  cout << -1 << endl;
}