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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using ll = long long;
using namespace std;

constexpr int MOD = 1e9 + 7;
constexpr ll MOD_LL = ll(1e9 + 7);

int main(void) {
  int h, w, Ch, Cw, Dh, Dw;
  cin >> h >> w >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;

  vector<string> s(h);
  for (int i = 0; i < h; ++i) {
    cin >> s[i];
  }

  vector<int> dx = {0, 1, 0, -1};
  vector<int> dy = {1, 0, -1, 0};

  constexpr ll INF = 1e9 + 10;
  vector<vector<ll>> dist(h, vector<ll>(w, INF));
  deque<pair<int, int>> q;

  dist[Ch][Cw] = 0;
  q.push_back({Ch, Cw});

  vector<pair<int, int>> restart;

  while (!q.empty()) {
    int nowx = q.front().first;
    int nowy = q.front().second;
    q.pop_front();

    for (int k = 0; k < 4; ++k) {
      int nx = nowx + dx[k];
      int ny = nowy + dy[k];

      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;

      if (dist[nx][ny] > dist[nowx][nowy]) {
        dist[nx][ny] = dist[nowx][nowy];
        q.push_front({nx, ny});
      }
    }

    for (int magicx = -2; magicx <= 2; ++magicx) {
      for (int magicy = -2; magicy <= 2; ++magicy) {
        int mx = nowx + magicx;
        int my = nowy + magicy;

        if (mx < 0 || mx >= h || my < 0 || my >= w)
          continue;
        if (s[mx][my] == '#')
          continue;

        if (dist[mx][my] > dist[nowx][nowy] + 1LL) {
          dist[mx][my] = dist[nowx][nowy] + 1LL;
          q.push_back({mx, my});
        }
      }
    }
  }

  if (dist[Dh][Dw] != INF) {
    cout << dist[Dh][Dw] << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using ll = long long;
using namespace std;

constexpr int MOD = 1e9 + 7;
constexpr ll MOD_LL = ll(1e9 + 7);

int main(void) {
  int h, w, Ch, Cw, Dh, Dw;
  cin >> h >> w >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;

  vector<string> s(h);
  for (int i = 0; i < h; ++i) {
    cin >> s[i];
  }

  vector<int> dx = {0, 1, 0, -1};
  vector<int> dy = {1, 0, -1, 0};

  constexpr ll INF = 1 << 30;
  vector<vector<ll>> dist(h, vector<ll>(w, 1 << 30));
  queue<pair<int, int>> q;

  dist[Ch][Cw] = 0;
  q.push({Ch, Cw});

  vector<pair<int, int>> restart;

  while (!q.empty()) {
    int nowx = q.front().first;
    int nowy = q.front().second;
    q.pop();

    for (int k = 0; k < 4; ++k) {
      int nx = nowx + dx[k];
      int ny = nowy + dy[k];

      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;

      if (dist[nx][ny] > dist[nowx][nowy]) {
        dist[nx][ny] = dist[nowx][nowy];
        q.push({nx, ny});
      }
    }

    for (int magicx = -2; magicx <= 2; ++magicx) {
      for (int magicy = -2; magicy <= 2; ++magicy) {
        int mx = nowx + magicx;
        int my = nowy + magicy;

        if (mx < 0 || mx >= h || my < 0 || my >= w)
          continue;
        if (s[mx][my] == '#')
          continue;

        if (dist[mx][my] > dist[nowx][nowy] + 1LL) {
          dist[mx][my] = dist[nowx][nowy] + 1LL;
          q.push({mx, my});
        }
      }
    }
  }

  if (dist[Dh][Dw] != INF) {
    cout << dist[Dh][Dw] << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}