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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long long, long long> pll;
typedef vector<pll> vpll;

typedef long double ld;
typedef vector<ld> vld;

typedef vector<bool> vb;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()

template <class T> void chmin(T &a, T b) { a = min(a, b); }
template <class T> void chmax(T &a, T b) { a = max(a, b); }

constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

void solve() {
  ll h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vvll d(h, vll(w, INFL));
  bool updated = true;
  typedef tuple<ll, ll, ll> tup;
  deque<tup> que;
  que.push_front(make_tuple(ch, cw, 0));
  d[ch][cw] = 0;
  while (!que.empty()) {
    ll ci, cj, cd;
    tie(ci, cj, cd) = que.front();
    que.pop_front();
    if (d[ci][cj] < cd)
      continue;
    for (int di = -2; di <= 2; ++di) {
      for (int dj = -2; dj <= 2; ++dj) {
        if (di == 0 && dj == 0)
          continue;
        ll ni = ci + di;
        ll nj = cj + dj;
        if (ni < 0 || ni >= h)
          continue;
        if (nj < 0 || nj >= w)
          continue;
        if (s[ni][nj] == '#')
          continue;
        ll dd = abs(di) + abs(dj) - 1;
        if (dd > 1)
          dd = 1;
        if (d[ni][nj] <= cd + dd)
          continue;
        d[ni][nj] = cd + dd;
        tup t = make_tuple(ni, nj, cd + dd);
        if (dd == 0)
          que.push_front(t);
        else
          que.push_back(t);
      }
    }
  }
  if (d[dh][dw] == INFL)
    d[dh][dw] = -1;
  cout << d[dh][dw] << endl;
  return;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long long, long long> pll;
typedef vector<pll> vpll;

typedef long double ld;
typedef vector<ld> vld;

typedef vector<bool> vb;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()

template <class T> void chmin(T &a, T b) { a = min(a, b); }
template <class T> void chmax(T &a, T b) { a = max(a, b); }

constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

void solve() {
  ll h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vvll d(h, vll(w, INFL));
  bool updated = true;
  typedef tuple<ll, ll, ll> tup;
  queue<tup> que;
  que.push(make_tuple(ch, cw, 0));
  d[ch][cw] = 0;
  while (!que.empty()) {
    ll ci, cj, cd;
    tie(ci, cj, cd) = que.front();
    que.pop();
    if (d[ci][cj] < cd)
      continue;
    for (int di = -2; di <= 2; ++di) {
      for (int dj = -2; dj <= 2; ++dj) {
        if (di == 0 && dj == 0)
          continue;
        ll ni = ci + di;
        ll nj = cj + dj;
        if (ni < 0 || ni >= h)
          continue;
        if (nj < 0 || nj >= w)
          continue;
        if (s[ni][nj] == '#')
          continue;
        ll dd = abs(di) + abs(dj) - 1;
        if (dd > 1)
          dd = 1;
        if (d[ni][nj] <= cd + dd)
          continue;
        d[ni][nj] = cd + dd;
        que.push(make_tuple(ni, nj, cd + dd));
      }
    }
  }
  if (d[dh][dw] == INFL)
    d[dh][dw] = -1;
  cout << d[dh][dw] << endl;
  return;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
}