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

#include <bits/stdc++.h>

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)

using namespace std;

using ll = long long;
using P = pair<int, int>;

const int INF = 1'000'000'000;

int dhh[] = {1, 0, -1, 0};
int dww[] = {0, 1, 0, -1};
int dhh2[] = {2, 2,  2,  2,  2,  1,  1,  1,  1,  0,
              0, -1, -1, -1, -1, -2, -2, -2, -2, -2};
int dww2[] = {2,  1, 0, -1, -2, 2, 1, -1, -2, 2,
              -2, 2, 1, -1, -2, 2, 1, 0,  -1, -2};

int main() {
  int h, w;
  int ch, cw;
  cin >> h >> w;
  cin >> ch >> cw;
  ch--;
  cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;
  vector<vector<char>> s(h, vector<char>(w));
  rep(i, h) {
    rep(j, w) { cin >> s.at(i).at(j); }
  }
  vector<vector<int>> d(h, vector<int>(w, INF));
  queue<P> que;
  queue<P> que2;
  que.push(P(ch, cw));
  d.at(ch).at(cw) = 0;
  while (!que.empty() || !que2.empty()) {
    P p;
    if (!que.empty()) {
      p = que.front();
      que.pop();
    } else {
      p = que2.front();
      que2.pop();
    }
    if (p.first == dh && p.second == dw) {
      continue;
    }
    rep(i, 4) {
      int ddh = dhh[i];
      int ddw = dww[i];
      if (ddh == 0 && ddw == 0) {
        continue;
      }
      int nh = p.first + ddh;
      int nw = p.second + ddw;
      if (nh < 0 || nh >= h) {
        continue;
      }
      if (nw < 0 || nw >= w) {
        continue;
      }
      if (s.at(nh).at(nw) != '.') {
        continue;
      }
      int cur = d.at(p.first).at(p.second);
      if (d.at(nh).at(nw) > cur) {
        que.push(P(nh, nw));
        d.at(nh).at(nw) = cur;
      }
    }
    rep(i, 20) {
      int ddh = dhh2[i];
      int ddw = dww2[i];
      if (ddh == 0 && ddw == 0) {
        continue;
      }
      int nh = p.first + ddh;
      int nw = p.second + ddw;
      if (nh < 0 || nh >= h) {
        continue;
      }
      if (nw < 0 || nw >= w) {
        continue;
      }
      if (s.at(nh).at(nw) != '.') {
        continue;
      }
      int cur = d.at(p.first).at(p.second);
      if (d.at(nh).at(nw) > cur + 1) {
        que2.push(P(nh, nw));
        d.at(nh).at(nw) = cur + 1;
      }
    }
  }

  int res = d.at(dh).at(dw);
  cout << (res == INF ? -1 : res) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)

using namespace std;

using ll = long long;
using P = pair<int, int>;

const int INF = 1'000'000'000;

int dhh[] = {1, 0, -1, 0};
int dww[] = {0, 1, 0, -1};

int main() {
  int h, w;
  int ch, cw;
  cin >> h >> w;
  cin >> ch >> cw;
  ch--;
  cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;
  vector<vector<char>> s(h, vector<char>(w));
  rep(i, h) {
    rep(j, w) { cin >> s.at(i).at(j); }
  }
  vector<vector<int>> d(h, vector<int>(w, INF));
  queue<P> que;
  queue<P> que2;
  que.push(P(ch, cw));
  d.at(ch).at(cw) = 0;
  while (!que.empty() || !que2.empty()) {
    P p;
    if (!que.empty()) {
      p = que.front();
      que.pop();
    } else {
      p = que2.front();
      que2.pop();
    }
    if (p.first == dh && p.second == dw) {
      continue;
    }
    for (int ddh = -2; ddh <= 2; ddh++) {
      for (int ddw = -2; ddw <= 2; ddw++) {
        if (ddh == 0 && ddw == 0) {
          continue;
        }
        bool isNear = false;
        rep(i, 4) {
          if (ddh == dhh[i] && ddw == dww[i]) {
            isNear = true;
            break;
          }
        }
        int nh = p.first + ddh;
        int nw = p.second + ddw;
        if (nh < 0 || nh >= h) {
          continue;
        }
        if (nw < 0 || nw >= w) {
          continue;
        }
        if (s.at(nh).at(nw) != '.') {
          continue;
        }
        int cur = d.at(p.first).at(p.second);
        if (isNear) {
          if (d.at(nh).at(nw) > cur) {
            que.push(P(nh, nw));
            d.at(nh).at(nw) = cur;
          }
        } else {
          if (d.at(nh).at(nw) > cur + 1) {
            que.push(P(nh, nw));
            d.at(nh).at(nw) = cur + 1;
          }
        }
      }
    }
  }

  int res = d.at(dh).at(dw);
  cout << (res == INF ? -1 : res) << endl;
  return 0;
}