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

#include <algorithm>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (llint i = (a); i < (n); i++)
#define ll long long
#define llint long long int
#define reverse(v) reverse(v.begin(), v.end());
#define Yes(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "Yes" << endl;                                                     \\
  else                                                                         \\
    cout << "No" << endl;
#define YES(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "YES" << endl;                                                     \\
  else                                                                         \\
    cout << "NO" << endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF UnionFind
#define Pint pair<int, int>
#define Pll pair<llint, llint>
#define keta(a) fixed << setprecision(a)
const ll mod = 1000000007;
// 辞書順はnext_permutation( begin( v ), end( v ) );やで！
const ll INF = 1000000000;

int main() {
  deque<Pint> a;
  int h, w;
  cin >> h >> w;
  hei(string) c(h);
  whei(int) visit(h, hei(int)(w, 0)), dist(h, hei(int)(w, 0));
  int x, y;
  cin >> x >> y;
  visit[x - 1][y - 1] = 2;
  a.push_front(Pint(x - 1, y - 1));
  cin >> x >> y;
  rep(i, 0, h) cin >> c[i];
  while (!a.empty()) {
    Pint v = a.front();
    a.pop_front();
    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};
    rep(i, 0, 4) {
      int Nx = dx[i] + v.first;
      int Ny = dy[i] + v.second;
      if (Nx < 0 || Ny < 0 || Nx >= h || Ny >= w)
        continue;
      if (visit[Nx][Ny] == 2 || c[Nx][Ny] == '#')
        continue;
      dist[Nx][Ny] = dist[v.first][v.second];
      visit[Nx][Ny] = 2;
      a.push_front(Pint(Nx, Ny));
    }
    rep(i, -2, 3) {
      rep(j, -2, 3) {
        int Nx = i + v.first;
        int Ny = j + v.second;
        if (Nx < 0 || Ny < 0 || Nx >= h || Ny >= w)
          continue;
        if (visit[Nx][Ny] || c[Nx][Ny] == '#')
          continue;
        dist[Nx][Ny] = dist[v.first][v.second] + 1;
        visit[Nx][Ny] = 1;
        a.push_back(Pint(Nx, Ny));
      }
    }
  }
  if (visit[x - 1][y - 1] == 0)
    cout << -1 << endl;
  else
    cout << dist[x - 1][y - 1] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define ll long long
#define llint long long int
#define reverse(v) reverse(v.begin(), v.end());
#define Yes(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "Yes" << endl;                                                     \\
  else                                                                         \\
    cout << "No" << endl;
#define YES(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "YES" << endl;                                                     \\
  else                                                                         \\
    cout << "NO" << endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF UnionFind
#define Pint pair<int, int>
#define Pll pair<llint, llint>
#define keta(a) fixed << setprecision(a)
constexpr auto mod = 1000000007;
// 辞書順はnext_permutation( begin( v ), end( v ) );やで！
const ll INF = (1LL << 63);

int main() {
  int h, w;
  cin >> h >> w;
  int sc, sd, gc, gd;
  cin >> sc >> sd >> gc >> gd;
  hei(string) a(h);
  rep(i, 0, h) cin >> a[i];
  sc--;
  sd--;
  gc--;
  gd--;
  queue<Pint> que;
  que.push(Pint(sc, sd));
  whei(int) s(h, hei(int)(w, -1));
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};
  s[sc][sd] = 0;
  while (!que.empty()) {
    Pint p = que.front();
    que.pop();
    rep(i, 0, 4) {
      int Nx = p.first + dx[i];
      int Ny = p.second + dy[i];
      if (Nx >= h || Nx < 0 || Ny >= w || Ny < 0 || a[Nx][Ny] == '#')
        continue;
      if (s[Nx][Ny] == -1) {
        s[Nx][Ny] = s[p.first][p.second];
        que.push(Pint(Nx, Ny));
      } else {
        if (s[Nx][Ny] > s[p.first][p.second]) {
          s[Nx][Ny] = s[p.first][p.second];
          que.push(Pint(Nx, Ny));
        }
      }
    }
    rep(i, -2, 3) {
      rep(j, -2, 3) {
        int Nx = p.first + i;
        int Ny = p.second + j;
        if (Nx >= h || Nx < 0 || Ny >= w || Ny < 0 || a[Nx][Ny] == '#')
          continue;
        if (s[Nx][Ny] == -1) {
          s[Nx][Ny] = s[p.first][p.second] + 1;
          que.push(Pint(Nx, Ny));
        } else {
          if (s[Nx][Ny] > (s[p.first][p.second] + 1)) {
            s[Nx][Ny] = s[p.first][p.second] + 1;
            que.push(Pint(Nx, Ny));
          }
        }
      }
    }
  }
  int ans = s[gc][gd];
  cout << ans << endl;

  return 0;
}