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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <new>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define lrep(i, n, m) for (int i = n; i < m; i++)

using ll = long long;
using ld = long double;
using Vii = vector<vector<int>>;
using Gjudge = vector<vector<bool>>;
using Vi = vector<int>;
using Vl = vector<ll>;
using Vd = vector<double>;
using Vld = vector<long double>;
using Vll = vector<vector<ll>>;
using Vldd = vector<vector<ld>>;
using Vs = vector<string>;
using Vb = vector<bool>;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Pli = pair<ll, int>;
using VPi = vector<Pi>;
using VPl = vector<Pl>;
using VPli = vector<Pli>;
using Ti = tuple<int, int, int>;
using Tl = tuple<ll, ll, ll>;

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

const int Mod = static_cast<int>(1e9 + 7);
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int h, w;
  Vs G;
  cin >> h >> w;
  int sx, sy;
  cin >> sx >> sy;
  sx--;
  sy--;
  int gx, gy;
  cin >> gx >> gy;
  gx--;
  gy--;
  G.resize(h);
  rep(i, h) cin >> G[i];

  // BFS のためのデータ構造
  Vii dist(h, Vi(w, 1e8)); // 全頂点を「未訪問」に初期化
  deque<Pi> que;

  // 初期条件 (頂点 0 を初期ノードとする)
  dist[sx][sy] = 0;
  que.push_back({sx, sy});

  // BFS 開始 (キューが空になるまで探索を行う)
  while (!que.empty()) {
    Pi v = que.front(); // キューから先頭頂点を取り出す
    que.pop_front();
    // v から辿れる頂点をすべて調べる
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        Pi nv = {v.first + i, v.second + j};
        if (nv.first < 0 || nv.first >= h || nv.second < 0 || nv.second >= w)
          continue;
        // if (dist[nv.first][nv.second] != 1e8)  continue;
        if (dist[nv.first][nv.second] <= dist[v.first][v.second])
          continue;
        if (G[nv.first][nv.second] == '#')
          continue;
        if ((i == -1 && j == 0) || (i == 0 && j == 1) || (i == 1 && j == 0) ||
            (i == 0 && j == -1)) {
          dist[nv.first][nv.second] = dist[v.first][v.second];
          que.push_front(nv);
        } else {
          dist[nv.first][nv.second] = dist[v.first][v.second] + 1;
          que.push_back(nv);
        }
        // cout << nv.first << " " << nv.second << endl;
      }
    }
  }

  if (dist[gx][gy] != 1e8) {
    cout << dist[gx][gy] << endl;
  } else {
    cout << -1 << endl;
  }

  // for (int i = 0; i < h; i++) {
  //     for (int j = 0; j < w; j++) {
  //         cout << dist[i][j] << " ";
  //     }
  //     cout << endl;
  // }
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <new>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define lrep(i, n, m) for (int i = n; i < m; i++)

using ll = long long;
using ld = long double;
using Vii = vector<vector<int>>;
using Gjudge = vector<vector<bool>>;
using Vi = vector<int>;
using Vl = vector<ll>;
using Vd = vector<double>;
using Vld = vector<long double>;
using Vll = vector<vector<ll>>;
using Vldd = vector<vector<ld>>;
using Vs = vector<string>;
using Vb = vector<bool>;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Pli = pair<ll, int>;
using VPi = vector<Pi>;
using VPl = vector<Pl>;
using VPli = vector<Pli>;
using Ti = tuple<int, int, int>;
using Tl = tuple<ll, ll, ll>;

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

const int Mod = static_cast<int>(1e9 + 7);
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int h, w;
  Vs G;
  cin >> h >> w;
  int sx, sy;
  cin >> sx >> sy;
  sx--;
  sy--;
  int gx, gy;
  cin >> gx >> gy;
  gx--;
  gy--;
  G.resize(h);
  rep(i, h) cin >> G[i];

  // BFS のためのデータ構造
  Vii dist(h, Vi(w, 1e8)); // 全頂点を「未訪問」に初期化
  queue<Pi> que;

  // 初期条件 (頂点 0 を初期ノードとする)
  dist[sx][sy] = 0;
  que.push({sx, sy});

  // BFS 開始 (キューが空になるまで探索を行う)
  while (!que.empty()) {
    Pi v = que.front(); // キューから先頭頂点を取り出す
    que.pop();
    // v から辿れる頂点をすべて調べる
    rep(i, 4) {
      Pi nv = {v.first + dx[i], v.second + dy[i]};
      if (nv.first < 0 || nv.first >= h || nv.second < 0 || nv.second >= w)
        continue;
      if (dist[nv.first][nv.second] <= dist[v.first][v.second])
        continue;
      if (G[nv.first][nv.second] == '#')
        continue;
      dist[nv.first][nv.second] = dist[v.first][v.second];
      que.push(nv);
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if ((i == -1 && j == 0) || (i == 0 && j == 1) || (i == 1 && j == 0) ||
            (i == 0 && j == -1))
          continue;
        Pi nv = {v.first + i, v.second + j};
        if (nv.first < 0 || nv.first >= h || nv.second < 0 || nv.second >= w)
          continue;
        // if (dist[nv.first][nv.second] != 1e8)  continue;
        if (dist[nv.first][nv.second] <= dist[v.first][v.second])
          continue;
        if (G[nv.first][nv.second] == '#')
          continue;

        dist[nv.first][nv.second] = dist[v.first][v.second] + 1;
        que.push(nv);
        // cout << nv.first << " " << nv.second << endl;
      }
    }
  }

  if (dist[gx][gy] != 1e8) {
    cout << dist[gx][gy] << endl;
  } else {
    cout << -1 << endl;
  }

  // for (int i = 0; i < h; i++) {
  //     for (int j = 0; j < w; j++) {
  //         cout << dist[i][j] << " ";
  //     }
  //     cout << endl;
  // }
}