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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef int itn;
const ll LINF = 1e18;
const int INF = 1e9;

// マクロ定義
#define vvint(vec, n, m, l)                                                    \\
  vector<vector<int>> vec(n, vector<int>(m, l)); // lで初期化
#define vvll(vec, n, m, l) vector<vector<ll>> vec(n, vector<ll>(m, l));
#define vint vector<int>
#define pint pair<int, int>
#define rep(i, a) for (ll i = 0; i < (a); i++)
#define all(x) (x).begin(), (x).end()
#define debug system("pause") // デバッグ用
#define ret return 0

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using Graph = vector<vector<ll>>;

#define ketasitei setprecision(15) // 15桁表示

const ll MOD = 1000000007;
const double PI = 3.1415926535897932;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll h, w;
  cin >> h >> w;
  ll c, d;
  cin >> c >> d;
  ll cc, dd;
  cin >> cc >> dd;

  vector<string> s(h);
  rep(i, h) { cin >> s[i]; }

  vector<vector<ll>> use(h, vector<ll>(w, LINF));

  deque<pair<ll, pair<ll, ll>>> q;
  pair<ll, ll> temp;
  temp.first = c - 1;
  temp.second = d - 1;
  q.emplace_back(0, temp);

  while (!q.empty()) {
    pair<ll, pair<ll, ll>> tmp = q.front();
    q.pop_front();

    if (s[tmp.second.first][tmp.second.second] == '#')
      continue;

    if (tmp.first < use[tmp.second.first][tmp.second.second]) {
      use[tmp.second.first][tmp.second.second] = tmp.first;

      for (int i = -2; i < 3; i++) {
        for (int j = -2; j < 3; j++) {
          if (i == 0 && j == 0)
            continue;
          if (tmp.second.first + i < 0 || h <= tmp.second.first + i)
            continue;
          if (tmp.second.second + j < 0 || w <= tmp.second.second + j)
            continue;

          temp.first = tmp.second.first + i;
          temp.second = tmp.second.second + j;

          q.emplace_back(tmp.first + 1, temp);
        }
      }
      if (0 <= tmp.second.first - 1) {
        temp.first = tmp.second.first - 1;
        temp.second = tmp.second.second;
        q.emplace_front(tmp.first, temp);
      }
      if (tmp.second.first + 1 < h) {
        temp.first = tmp.second.first + 1;
        temp.second = tmp.second.second;
        q.emplace_front(tmp.first, temp);
      }
      if (0 <= tmp.second.second - 1) {
        temp.first = tmp.second.first;
        temp.second = tmp.second.second - 1;
        q.emplace_front(tmp.first, temp);
      }
      if (tmp.second.second + 1 < w) {
        temp.first = tmp.second.first;
        temp.second = tmp.second.second + 1;
        q.emplace_front(tmp.first, temp);
      }

    } else {
      continue;
    }
  }
  if (use[cc - 1][dd - 1] == LINF) {
    cout << -1 << endl;
    ret;
  }
  cout << use[cc - 1][dd - 1] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef int itn;
const ll LINF = 1e18;
const int INF = 1e9;

// マクロ定義
#define vvint(vec, n, m, l)                                                    \\
  vector<vector<int>> vec(n, vector<int>(m, l)); // lで初期化
#define vvll(vec, n, m, l) vector<vector<ll>> vec(n, vector<ll>(m, l));
#define vint vector<int>
#define pint pair<int, int>
#define rep(i, a) for (ll i = 0; i < (a); i++)
#define all(x) (x).begin(), (x).end()
#define debug system("pause") // デバッグ用
#define ret return 0

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using Graph = vector<vector<ll>>;

#define ketasitei setprecision(15) // 15桁表示

const ll MOD = 1000000007;
const double PI = 3.1415926535897932;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll h, w;
  cin >> h >> w;
  ll c, d;
  cin >> c >> d;
  ll cc, dd;
  cin >> cc >> dd;

  vector<string> s(h);
  rep(i, h) { cin >> s[i]; }

  vector<vector<ll>> use(h, vector<ll>(w, LINF));

  stack<pair<ll, pair<ll, ll>>> q;
  pair<ll, ll> temp;
  temp.first = c - 1;
  temp.second = d - 1;
  q.emplace(0, temp);

  while (!q.empty()) {
    pair<ll, pair<ll, ll>> tmp = q.top();
    q.pop();

    if (s[tmp.second.first][tmp.second.second] == '#')
      continue;

    if (tmp.first < use[tmp.second.first][tmp.second.second]) {
      use[tmp.second.first][tmp.second.second] = tmp.first;

      for (int i = -2; i < 3; i++) {
        for (int j = -2; j < 3; j++) {
          if (i == 0 && j == 0)
            continue;
          if (tmp.second.first + i < 0 || h <= tmp.second.first + i)
            continue;
          if (tmp.second.second + j < 0 || w <= tmp.second.second + j)
            continue;

          temp.first = tmp.second.first + i;
          temp.second = tmp.second.second + j;

          q.emplace(tmp.first + 1, temp);
        }
      }

      if (0 <= tmp.second.first - 1) {
        temp.first = tmp.second.first - 1;
        temp.second = tmp.second.second;
        q.emplace(tmp.first, temp);
      }
      if (tmp.second.first + 1 < h) {
        temp.first = tmp.second.first + 1;
        temp.second = tmp.second.second;
        q.emplace(tmp.first, temp);
      }
      if (0 <= tmp.second.second - 1) {
        temp.first = tmp.second.first;
        temp.second = tmp.second.second - 1;
        q.emplace(tmp.first, temp);
      }
      if (tmp.second.second + 1 < w) {
        temp.first = tmp.second.first;
        temp.second = tmp.second.second + 1;
        q.emplace(tmp.first, temp);
      }

    } else {
      continue;
    }
  }
  if (use[cc - 1][dd - 1] == LINF) {
    cout << -1 << endl;
    ret;
  }
  cout << use[cc - 1][dd - 1] << endl;

  return 0;
}