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

#include <deque>
#include <iostream>
using namespace std;
int H, W, Ch, Cw, Dh, Dw;
int main() {
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--, Cw--;
  Dh--, Dw--;
  char S[H][W];
  for (int i = 0; i < H * W; i++)
    cin >> S[i / W][i % W];
  int d[H][W];
  for (int i = 0; i < H * W; i++)
    d[i / W][i % W] = 1000000000;
  // bool visited[H][W];
  // for(int i=0;i<H*W;i++)visited[i/W][i%W]=false;

  deque<pair<int, int>> q;
  q.push_front(make_pair(Ch, Cw));
  d[Ch][Cw] = 0;
  // visited[Ch][Cw]=true;
  while (!q.empty()) {
    auto position = q.front();
    q.pop_front();
    int x = position.first, y = position.second;
    int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
    for (int i = 0; i < 4; i++) {
      if (x + dx[i] < 0 || H <= x + dx[i] || y + dy[i] < 0 || W <= y + dy[i])
        continue;
      if (S[x + dx[i]][y + dy[i]] == '#')
        continue;
      if (d[x + dx[i]][y + dy[i]] > d[x][y]) {
        d[x + dx[i]][y + dy[i]] = d[x][y];
        q.push_front(make_pair(x + dx[i], y + dy[i]));
      }
    }

    for (int i = 0; i < 5 * 5; i++) {
      int _dx = i / 5 - 2, _dy = i % 5 - 2;
      if (x + _dx < 0 || H <= x + _dx || y + _dy < 0 || W <= y + _dy)
        continue;
      if (S[x + _dx][y + _dy] == '#')
        continue;
      if (d[x + _dx][y + _dy] > d[x][y] + 1) {
        d[x + _dx][y + _dy] = d[x][y] + 1;
        q.push_back(make_pair(x + _dx, y + _dy));
        // cout<<x+_dx<<" "<<y+_dy<<endl;
      }
    }
    // for(int i=0;i<H*W;i++)cout<<d[i/W][i%W]<<" \\n"[i%W==W-1];
  }
  cout << (d[Dh][Dw] == 1000000000 ? -1 : d[Dh][Dw]) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
int H, W, Ch, Cw, Dh, Dw;
int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--, Cw--;
  Dh--, Dw--;
  char S[H][W];
  for (int i = 0; i < H * W; i++)
    cin >> S[i / W][i % W];
  int d[H][W];
  for (int i = 0; i < H * W; i++)
    d[i / W][i % W] = 1000000000;
  // bool visited[H][W];
  // for(int i=0;i<H*W;i++)visited[i/W][i%W]=false;

  queue<pair<int, int>> q;
  q.push(make_pair(Ch, Cw));
  d[Ch][Cw] = 0;
  // visited[Ch][Cw]=true;
  while (!q.empty()) {
    auto position = q.front();
    q.pop();
    int x = position.first, y = position.second;
    int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
    for (int i = 0; i < 4; i++) {
      if (x + dx[i] < 0 || H <= x + dx[i] || y + dy[i] < 0 || W <= y + dy[i])
        continue;
      if (S[x + dx[i]][y + dy[i]] == '#')
        continue;
      if (d[x + dx[i]][y + dy[i]] > d[x][y]) {
        d[x + dx[i]][y + dy[i]] = d[x][y];
        q.push(make_pair(x + dx[i], y + dy[i]));
      }
    }

    for (int i = 0; i < 5 * 5; i++) {
      int _dx = i / 5 - 2, _dy = i % 5 - 2;
      if (x + _dx < 0 || H <= x + _dx || y + _dy < 0 || W <= y + _dy)
        continue;
      if (S[x + _dx][y + _dy] == '#')
        continue;

      for (int j = 0; j < 4; j++) {
        if (d[x + dx[j]][y + dy[j]] <= d[x + _dx][y + _dy])
          continue;
      }
      if (d[x + _dx][y + _dy] > d[x][y] + 1) {
        d[x + _dx][y + _dy] = d[x][y] + 1;
        q.push(make_pair(x + _dx, y + _dy));
        // cout<<x+_dx<<" "<<y+_dy<<endl;
      }
    }
    // for(int i=0;i<H*W;i++)cout<<d[i/W][i%W]<<" \\n"[i%W==W-1];
  }
  cout << (d[Dh][Dw] == 1000000000 ? -1 : d[Dh][Dw]) << endl;
  return 0;
}