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

#include "bits/stdc++.h"

using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int ddx[] = {0,  1,  1, 0,  2, 1,  -1, -2, 2,  2,
                   -2, -2, 1, -1, 1, -1, 2,  2,  -2, -2};
const int ddy[] = {2, 1,  -1, -2, 0,  1,  1, 0,  2, -2,
                   2, -2, 2,  2,  -2, -2, 1, -1, 1, -1};

int main() {
  int H, W;
  cin >> H >> W;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> S(H);
  for (int n = 0; n < H; ++n) {
    cin >> S[n];
  }

  vector<vector<int>> D(H, vector<int>(W, 1e9)); // 回数
  deque<pair<int, int>> q;
  q.push_front(make_pair(ch, cw));
  D[ch][cw] = 0;

  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop_front();
    // 範囲内かつ0を4セット
    for (int l = 0; l < 4; ++l) {
      int x1 = x + dx[l];
      int y1 = y + dy[l];
      if ((0 <= x1) && (H > x1) && (0 <= y1) && (W > y1) &&
          ('.' == S[x1][y1])) {
        if (D[x1][y1] > D[x][y]) {
          D[x1][y1] = D[x][y];
          q.push_front(make_pair(x1, y1));
        }
      }
    }
    // 範囲内かつ0を20セット
    for (int l = 0; l < 20; ++l) {
      int x1 = x + ddx[l];
      int y1 = y + ddy[l];
      if ((0 <= x1) && (H > x1) && (0 <= y1) && (W > y1) &&
          ('.' == S[x1][y1])) {
        if (D[x1][y1] > D[x][y] + 1) {
          D[x1][y1] = D[x][y] + 1;
          q.push_back(make_pair(x1, y1));
        }
      }
    }
  }
  if (1e9 == D[dh][dw]) {
    cout << -1 << endl;
  } else {
    cout << D[dh][dw] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

const int ddx[] = {1,  1,  1, -1, -2, 2,  0, 0, 2,  2,
                   -2, -2, 1, 1,  -1, -1, 2, 2, -2, -2};
const int ddy[] = {1, -1, 1, 1,  0, 0,  2, -2, 1, -1,
                   1, -1, 2, -2, 2, -2, 2, -2, 2, -2};

int main() {
  int H, W;
  cin >> H >> W;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> S(H);
  for (int n = 0; n < H; ++n) {
    cin >> S[n];
  }

  vector<vector<int>> D(H, vector<int>(W, 1e9)); // 回数
  queue<pair<int, pair<int, int>>> q;
  q.push(make_pair(0, make_pair(ch, cw)));
  D[ch][cw] = 0;

  while (!q.empty()) {
    int x = q.front().second.first;
    int y = q.front().second.second;
    int z = q.front().first;
    q.pop();
    // 範囲内かつ0を4セット
    for (int l = 0; l < 4; ++l) {
      int x1 = x + dx[l];
      int y1 = y + dy[l];
      if ((0 <= x1) && (H > x1) && (0 <= y1) && (W > y1) &&
          ('.' == S[x1][y1])) {
        if (D[x1][y1] > D[x][y]) {
          D[x1][y1] = D[x][y];
          pair<int, pair<int, int>> p = make_pair(D[x1][y1], make_pair(x1, y1));
          q.emplace(p);
        }
      }
    }
    // 範囲内かつ0を24セット
    for (int l = 0; l < 20; ++l) {
      int x1 = x + ddx[l];
      int y1 = y + ddy[l];
      if ((0 <= x1) && (H > x1) && (0 <= y1) && (W > y1) &&
          ('.' == S[x1][y1])) {
        if (D[x1][y1] > D[x][y]) {
          D[x1][y1] = D[x][y] + 1;
          pair<int, pair<int, int>> p = make_pair(D[x1][y1], make_pair(x1, y1));
          q.emplace(p);
        }
      }
    }
  }
  if (1e9 == D[dh][dw]) {
    cout << -1 << endl;
  } else {
    cout << D[dh][dw] << endl;
  }
  return 0;
}