Problem p02579 (C++)

Buggy submission (status: 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};

struct dat {
  int d, r, c;
};

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)); // 回数
  auto c = [](dat a, dat b) { return a.d > b.d; };
  priority_queue<dat, vector<dat>, decltype(c)> q(c);
  q.push({0, ch, cw});
  D[ch][cw] = 0;

  while (!q.empty()) {
    int x = q.top().r;
    int y = q.top().c;
    int z = q.top().d;
    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];
          q.push({D[x1][y1], 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]) {
          D[x1][y1] = D[x][y] + 1;
          q.push({D[x1][y1], x1, y1});
        }
      }
    }
  }
  if (1e9 == D[dh][dw]) {
    cout << -1 << endl;
  } else {
    cout << D[dh][dw] << endl;
  }
  return 0;
}


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[] = {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};

struct dat {
  int d, r, c;
};

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)); // 回数
  auto c = [](dat a, dat b) { return a.d > b.d; };
  priority_queue<dat, vector<dat>, decltype(c)> q(c);
  q.push({0, ch, cw});
  D[ch][cw] = 0;

  while (!q.empty()) {
    int x = q.top().r;
    int y = q.top().c;
    int z = q.top().d;
    q.pop();
    if (D[x][y] < z)
      continue;
    if (x == dh && y == dw)
      continue;
    // 範囲内かつ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({D[x1][y1], 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]) {
          D[x1][y1] = D[x][y] + 1;
          q.push({D[x1][y1], x1, y1});
        }
      }
    }
  }
  if (1e9 == D[dh][dw]) {
    cout << -1 << endl;
  } else {
    cout << D[dh][dw] << endl;
  }
  return 0;
}