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

#include <bits/stdc++.h>
#define M_PI 3.14159265358979323846
#define Speed_UP                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define ff first
#define ss second
#define sz(x) (int)x.size()
#warning Remeber to change t
using namespace std;
typedef long long ll;
inline void setIO(string name = "") {
#ifndef ONLINE_JUDGE
  freopen((name + ".in").c_str(), "r", stdin);
  freopen((name + ".out").c_str(), "w", stdout);
#endif
}
ll const INF = 1e6 + 7;
ll const mx = 1e3 + 7;
ll h, w, c_h, c_w, d_h, d_w;
char maze[mx][mx];
vector<ll> adj[mx];
ll dis[mx][mx];
int main() {
  Speed_UP ll t;
  t = 1;
  while (t--) {
    cin >> h >> w;
    cin >> c_h >> c_w >> d_h >> d_w;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> maze[i][j];
        dis[i][j] = INF;
      }
    }
    // source ki distance
    dis[c_h][c_w] = 0;
    deque<pair<ll, ll>> q;
    q.pb(make_pair(c_h, c_w));

    while (!q.empty()) {
      ll x, y;
      x = q.front().ff;
      y = q.front().ss;
      // cout<<x<<" "<<y<<"\\n";
      q.pop_front();

      // x-2//x+2//y-2//y+2
      for (int i = x - 2; i <= x + 2; i++) {
        for (int j = y - 2; j <= y + 2; j++) {
          if (i >= 1 && i <= h && j >= 1 && j <= w && maze[i][j] == '.') {
            // adjacent abs diff sum will be 1
            if (abs(i - x) + abs(j - y) <= 1) {
              if (dis[x][y] + 0 < dis[i][j]) {
                dis[i][j] = dis[x][y];
                q.push_front(make_pair(i, j));
              }
            } else {
              if (dis[x][y] + 1 < dis[i][j]) {
                dis[i][j] = dis[x][y] + 1;
                q.push_back({i, j});
              }
            }
          }
        }
      }
    }
    if (dis[d_h][d_w] != INF)
      cout << dis[d_h][d_w];
    else
      cout << "-1";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define M_PI 3.14159265358979323846
#define Speed_UP                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define ff first
#define ss second
#define sz(x) (int)x.size()
#warning Remeber to change t
using namespace std;
typedef long long ll;
inline void setIO(string name = "") {
#ifndef ONLINE_JUDGE
  freopen((name + ".in").c_str(), "r", stdin);
  freopen((name + ".out").c_str(), "w", stdout);
#endif
}
ll const INF = 1e6 + 7;
ll const mx = 1e3 + 7;
ll h, w, c_h, c_w, d_h, d_w;
char maze[mx][mx];
vector<ll> adj[mx];
map<pair<ll, ll>, ll> dis;
int main() {
  Speed_UP ll t;
  t = 1;
  while (t--) {
    cin >> h >> w;
    cin >> c_h >> c_w >> d_h >> d_w;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> maze[i][j];
        dis[{i, j}] = INF;
      }
    }
    // source ki distance
    dis[{c_h, c_w}] = 0;
    deque<pair<ll, ll>> q;
    q.pb(make_pair(c_h, c_w));

    while (!q.empty()) {
      ll x, y;
      x = q.front().ff;
      y = q.front().ss;
      // cout<<x<<" "<<y<<"\\n";
      q.pop_front();

      // x-2//x+2//y-2//y+2
      for (int i = x - 2; i <= x + 2; i++) {
        for (int j = y - 2; j <= y + 2; j++) {
          if (i >= 1 && i <= h && j >= 1 && j <= w && maze[i][j] == '.') {
            // adjacent abs diff sum will be 1
            if (abs(i - x) + abs(j - y) <= 1) {
              if (dis[{x, y}] + 0 < dis[{i, j}]) {
                dis[{i, j}] = dis[{x, y}];
                q.push_front(make_pair(i, j));
              }
            } else {
              if (dis[{x, y}] + 1 < dis[{i, j}]) {
                dis[{i, j}] = dis[{x, y}] + 1;
                q.push_back({i, j});
              }
            }
          }
        }
      }
    }
    if (dis[{d_h, d_w}] != INF)
      cout << dis[{d_h, d_w}];
    else
      cout << "-1";
  }
}