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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, n) for (int64_t i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
#define ketasuu(n) fixed << setprecision(n)
#define btoe(p) p.begin(), p.end()
#define etob(p) p.rbegin(), p.rend()

const vector<P> dx = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const vector<P> ddx = {{-2, -2}, {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2},
                       {-1, -2}, {-1, -1}, {-1, 1}, {-1, 2}, {0, -2},
                       {0, 2},   {1, -2},  {1, -1}, {1, 1},  {1, 2},
                       {2, -2},  {2, -1},  {2, 0},  {2, 1},  {2, 2}};

int main() {
  int h, w;
  cin >> h >> w;
  P c, d;
  cin >> c.first >> c.second >> d.first >> d.second;
  c.first--;
  c.second--;
  d.first--;
  d.second--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> p(h, vector<int>(w, -1));
  deque<P> che, che2;
  che.push_back(c);
  che2.push_back(c);
  p[c.first][c.second] = 0;
  int now = 0;
  while (!che.empty()) {
    now++;
    while (!che.empty()) {
      rep(i, 4) {
        P kari = che[0];
        kari.first += dx[i].first;
        kari.second += dx[i].second;
        if (0 <= kari.first && kari.first < h && 0 <= kari.second &&
            kari.second < w && s[kari.first][kari.second] == '.') {
          if (p[kari.first][kari.second] <= p[che[0].first][che[0].second] &&
              p[kari.first][kari.second] != -1)
            continue;
          che.push_back(kari);
          che2.push_back(kari);
          p[kari.first][kari.second] = p[che[0].first][che[0].second];
        }
      }
      che2.push_back(che[0]);
      che.pop_front();
    }
    for (auto &v : che2) {
      rep(i, 20) {
        P kari = v;
        kari.first += ddx[i].first;
        kari.second += ddx[i].second;
        if (0 <= kari.first && kari.first < h && 0 <= kari.second &&
            kari.second < w && s[kari.first][kari.second] == '.') {
          if (p[kari.first][kari.second] == -1) {
            p[kari.first][kari.second] = now;
            che.push_back(kari);
          }
        }
      }
    }
    che2.clear();
  }
  cout << p[d.first][d.second] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, n) for (int64_t i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
#define ketasuu(n) fixed << setprecision(n)
#define btoe(p) p.begin(), p.end()
#define etob(p) p.rbegin(), p.rend()

const vector<P> dx = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const vector<P> ddx = {{-2, -2}, {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2},
                       {-1, -2}, {-1, -1}, {-1, 1}, {-1, 2}, {0, -2},
                       {0, 2},   {1, -2},  {1, -1}, {1, 1},  {1, 2},
                       {2, -2},  {2, -1},  {2, 0},  {2, 1},  {2, 2}};

int main() {
  int h, w;
  cin >> h >> w;
  P c, d;
  cin >> c.first >> c.second >> d.first >> d.second;
  c.first--;
  c.second--;
  d.first--;
  d.second--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> p(h, vector<int>(w, -1));
  deque<P> che;
  che.push_back(c);
  p[c.first][c.second] = 0;
  while (!che.empty()) {
    rep(i, 4) {
      P kari = che[0];
      kari.first += dx[i].first;
      kari.second += dx[i].second;
      if (0 <= kari.first && kari.first < h && 0 <= kari.second &&
          kari.second < w && s[kari.first][kari.second] == '.') {
        if (p[kari.first][kari.second] <= p[che[0].first][che[0].second] &&
            p[kari.first][kari.second] != -1)
          continue;
        che.push_back(kari);
        p[kari.first][kari.second] = p[che[0].first][che[0].second];
      }
    }
    for (auto &v : ddx) {
      int i = v.first, j = v.second;
      P kari = che[0];
      kari.first += i;
      kari.second += j;
      if (0 <= kari.first && kari.first < h && 0 <= kari.second &&
          kari.second < w && s[kari.first][kari.second] == '.') {
        if (p[kari.first][kari.second] <=
                (p[che[0].first][che[0].second] + 1) &&
            p[kari.first][kari.second] != -1)
          continue;
        p[kari.first][kari.second] = p[che[0].first][che[0].second] + 1;
        che.push_back(kari);
      }
    }
    che.pop_front();
  }
  cout << p[d.first][d.second] << endl;
  return 0;
}