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

//@author :  |< U $ |-|
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define ll long long
// #define pb push_back
// #define pop pop_back
#define pi pair<long long, long long>
#define mp make_pair
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define jaldi                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define inm1(b) memset(b, -1, sizeof(b))
#define in0(b) memset(b, 0, sizeof(b))
#define ininf(b) memset(b, 1000000001, sizeof(b))
#define MOD 1e9 + 7
#define inf 10000000001
#define d1(a) cout << a << " " << endl
#define d2(a, b) cout << a << " " << b << endl
#define d3(a, b, c) cout << a << " " << b << " " << c << endl
#define TakeInput                                                              \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
using namespace std;

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

int main() {

  jaldi
#ifndef ONLINE_JUDGE
      TakeInput
#endif

          ll h,
      w, sx, sy, ex, ey;
  ll i, j, k;

  cin >> h >> w >> sx >> sy >> ex >> ey;
  string a[h];
  sx--;
  sy--;
  ex--;
  ey--;
  for (i = 0; i < h; i++)
    cin >> a[i];

  deque<pi> q;
  q.push_back(mp(sx, sy));
  vector<vector<ll>> dis(h + 1, vector<ll>(w + 1, inf));

  dis[sx][sy] = 0;
  while (!q.empty()) {

    ll cx = q.front().F;
    ll cy = q.front().S;

    ll r[] = {1, -1, 0, 0};
    ll c[] = {0, 0, 1, -1};

    q.pop_front();

    // d2(cx,cy);
    for (i = 0; i < 4; i++) {
      ll nx = cx + r[i];
      ll ny = cy + c[i];
      if (nx < h && ny < w && ny >= 0 && nx >= 0 && a[nx][ny] != '#') {
        if (dis[nx][ny] > dis[cx][cy]) {
          dis[nx][ny] = dis[cx][cy];
          q.push_front(mp(nx, ny));
        }
      }
    }

    for (i = cx - 2; i <= cx + 2; i++) {
      for (j = cy - 2; j <= cy + 2; j++) {

        if (i < h && j < w && j >= 0 && i >= 0 && a[i][j] != '#') {
          if (dis[i][j] > dis[cx][cy] + 1) {
            dis[i][j] = dis[cx][cy] + 1;
            q.push_back(mp(i, j));
          }
        }
      }
    }
  }

  cout << (dis[ex][ey] == inf ? -1 : dis[ex][ey]);
}


Error: TLE

Fixed submission:

//@author :  |< U $ |-|
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define ll long long
// #define pb push_back
// #define pop pop_back
#define pi pair<long long, long long>
#define mp make_pair
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define jaldi                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define inm1(b) memset(b, -1, sizeof(b))
#define in0(b) memset(b, 0, sizeof(b))
#define ininf(b) memset(b, 1000000001, sizeof(b))
#define MOD 1e9 + 7
#define inf 10000000001
#define d1(a) cout << a << " " << endl
#define d2(a, b) cout << a << " " << b << endl
#define d3(a, b, c) cout << a << " " << b << " " << c << endl
#define TakeInput                                                              \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
using namespace std;

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

int main() {

  jaldi
#ifndef ONLINE_JUDGE
      TakeInput
#endif

          ll h,
      w, sx, sy, ex, ey;
  ll i, j, k;

  cin >> h >> w >> sx >> sy >> ex >> ey;
  string a[h];
  sx--;
  sy--;
  ex--;
  ey--;
  for (i = 0; i < h; i++)
    cin >> a[i];

  queue<pi> q;
  q.push(mp(sx, sy));
  vector<vector<ll>> dis(h + 1, vector<ll>(w + 1, inf));

  dis[sx][sy] = 0;
  while (!q.empty()) {

    ll cx = q.front().F;
    ll cy = q.front().S;

    ll r[] = {1, -1, 0, 0};
    ll c[] = {0, 0, 1, -1};

    q.pop();

    // d2(cx,cy);
    for (i = 0; i < 4; i++) {
      ll nx = cx + r[i];
      ll ny = cy + c[i];
      if (nx < h && ny < w && ny >= 0 && nx >= 0 && a[nx][ny] != '#') {
        if (dis[nx][ny] > dis[cx][cy]) {
          dis[nx][ny] = dis[cx][cy];
          q.push(mp(nx, ny));
        }
      }
    }

    for (i = cx - 2; i <= cx + 2; i++) {
      for (j = cy - 2; j <= cy + 2; j++) {

        if (i < h && j < w && j >= 0 && i >= 0 && a[i][j] != '#') {
          if (dis[i][j] > dis[cx][cy] + 1) {
            dis[i][j] = dis[cx][cy] + 1;
            q.push(mp(i, j));
          }
        }
      }
    }
  }

  cout << (dis[ex][ey] == inf ? -1 : dis[ex][ey]);
}