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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef pair<ll, ll> P;

const int mod =
    1000000007; // 出力は (ans % mod + mod) % mod  (負の剰余を正にする)
const int inf = 1e9;
const long long INF = 1LL << 60; // INF = 11

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

int wx[20] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
              2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};
int wy[20] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
              0,  1,  1,  1,  1,  2,  2,  2,  2,  2};

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;
  char s[h][w];
  int dist[h][w];
  bool visit[h][w];
  rep(i, h) rep(j, w) {
    cin >> s[i][j];
    dist[i][j] = inf;
    visit[i][j] = false;
  }

  deque<P> que;
  que.push_back({ch, cw});
  visit[ch][cw] = true;
  dist[ch][cw] = 0;

  while (!que.empty()) {
    P p = que.front();
    que.pop_front();
    int y = p.first, x = p.second;
    rep(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];
      if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == '.' &&
          !visit[ny][nx]) {
        que.emplace_front(ny, nx);
        dist[ny][nx] = dist[y][x];
        visit[ny][nx] = true;
      }
    }
    rep(i, 20) {
      int ny = y + wy[i], nx = x + wx[i];
      if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == '.' &&
          !visit[ny][nx]) {
        if (dist[ny][nx] > dist[y][x] + 1) {
          que.push_back({ny, nx});
          dist[ny][nx] = dist[y][x] + 1;
        }
      }
    }
  }

  if (dist[dh][dw] == inf) {
    cout << -1 << endl;
  } else {
    cout << dist[dh][dw] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef pair<ll, ll> P;

const int mod =
    1000000007; // 出力は (ans % mod + mod) % mod  (負の剰余を正にする)
const int inf = 1e9;
const long long INF = 1LL << 60; // INF = 11

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

int wx[20] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
              2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};
int wy[20] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
              0,  1,  1,  1,  1,  2,  2,  2,  2,  2};

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;
  char s[h][w];
  int dist[h][w];
  bool visit[h][w];
  rep(i, h) rep(j, w) {
    dist[i][j] = inf;
    visit[i][j] = false;
  }
  rep(i, h) rep(j, w) cin >> s[i][j];
  queue<P> que;
  que.push({ch, cw});
  visit[ch][cw] = true;
  dist[ch][cw] = 0;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int y = p.first, x = p.second;
    for (int i = 0; i < 24; i++) {
      if (i < 4) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if (0 <= ny && ny < h && 0 <= nx && nx < w &&
            dist[ny][nx] > dist[y][x] && s[ny][nx] == '.') {
          visit[ny][nx] = true;
          dist[ny][nx] = dist[y][x];
          que.push({ny, nx});
        }
      } else {
        int ny = y + wy[i - 4];
        int nx = x + wx[i - 4];

        if (0 <= ny && ny < h && 0 <= nx && nx < w &&
            dist[ny][nx] > dist[y][x] + 1 && s[ny][nx] == '.') {
          visit[ny][nx] = true;
          dist[ny][nx] = min(dist[ny][nx], dist[y][x] + 1);
          que.push({ny, nx});
        }
      }
    }
  }
  if (dist[dh][dw] == inf) {
    cout << -1 << endl;
  } else {
    cout << dist[dh][dw] << endl;
  }
}