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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll, ll> P;
typedef pair<int, int> pii;
typedef vector<P> vpl;
typedef tuple<ll, ll, ll> tapu;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 510000;
ll dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
ll dx[8] = {0, 0, 1, -1, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T> inline void print(T &a) {
  rep(i, a.size()) cout << a[i] << " ";
  cout << "\\n";
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << a << " " << b << "\\n";
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << "\\n";
}
ll pcount(ll x) { return __builtin_popcountll(x); }
const int mod = 1e9 + 7;
// const int mod = 998244353;

bool in(ll y, ll x, ll h, ll w) { return 0 <= y && y < h && 0 <= x && x < w; }

int main() {
  int h, w;
  cin >> h >> w;
  int sy, sx;
  cin >> sy >> sx;
  sy--;
  sx--;
  int gy, gx;
  cin >> gy >> gx;
  gy--;
  gx--;
  vs s(h);
  rep(i, h) cin >> s[i];
  vvl d(h, vl(w, inf));
  d[sy][sx] = 0;
  priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<pair<ll, pii>>>
      q;
  q.emplace(0, pii(sy, sx));
  while (!q.empty()) {
    auto p = q.top();
    q.pop();
    int y = p.second.first;
    int x = p.second.second;
    if (d[y][x] < p.first)
      continue;
    rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (!in(ny, nx, h, w) || s[ny][nx] == '#')
        continue;
      if (d[ny][nx] > d[y][x]) {
        d[ny][nx] = d[y][x];
        q.emplace(d[ny][nx], pii(ny, nx));
      }
    }
    for (int ny = y - 2; ny <= y + 2; ny++) {
      for (int nx = x - 2; nx <= x + 2; nx++) {
        if (ny == y && nx == x)
          continue;
        if (!in(ny, nx, h, w) || s[ny][nx] == '#')
          continue;
        if (d[ny][nx] > d[y][x] + 1) {
          d[ny][nx] = d[y][x] + 1;
          q.emplace(d[ny][nx], pii(ny, nx));
        }
      }
    }
  }
  if (d[gy][gx] == inf)
    puts("-1");
  else
    cout << d[gy][gx] << "\\n";
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll, ll> P;
typedef pair<int, int> pii;
typedef vector<P> vpl;
typedef tuple<ll, ll, ll> tapu;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 510000;
ll dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
ll dx[8] = {0, 0, 1, -1, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T> inline void print(T &a) {
  rep(i, a.size()) cout << a[i] << " ";
  cout << "\\n";
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << a << " " << b << "\\n";
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << "\\n";
}
ll pcount(ll x) { return __builtin_popcountll(x); }
const int mod = 1e9 + 7;
// const int mod = 998244353;

bool in(ll y, ll x, ll h, ll w) { return 0 <= y && y < h && 0 <= x && x < w; }

int main() {
  int h, w;
  cin >> h >> w;
  int sy, sx;
  cin >> sy >> sx;
  sy--;
  sx--;
  int gy, gx;
  cin >> gy >> gx;
  gy--;
  gx--;
  vs s(h);
  rep(i, h) cin >> s[i];
  vvl d(h, vl(w, inf));
  d[sy][sx] = 0;
  queue<P> q;
  q.emplace(sy, sx);
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int y = p.first;
    int x = p.second;
    rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (!in(ny, nx, h, w) || s[ny][nx] == '#')
        continue;
      if (d[ny][nx] > d[y][x]) {
        d[ny][nx] = d[y][x];
        q.emplace(ny, nx);
      }
    }
    for (int ny = y - 2; ny <= y + 2; ny++) {
      for (int nx = x - 2; nx <= x + 2; nx++) {
        if (ny == y && nx == x)
          continue;
        if (!in(ny, nx, h, w) || s[ny][nx] == '#')
          continue;
        if (d[ny][nx] > d[y][x] + 1) {
          q.emplace(ny, nx);
          d[ny][nx] = d[y][x] + 1;
        }
      }
    }
  }
  if (d[gy][gx] == inf)
    puts("-1");
  else
    cout << d[gy][gx] << "\\n";
}