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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  // Failed to predict input format
  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];

  ll inf = 1e9;
  ll dh[] = {-1, 1, 0, 0};
  ll dw[] = {0, 0, -1, 1};

  vector<vector<ll>> cost(H, vector<ll>(W, inf));
  cost[Ch][Cw] = 0;

  deque<pair<ll, ll>> d;
  d.push_back({Ch, Cw});

  pair<ll, ll> now;
  ll now_h, now_w, next_h, next_w;

  while (!d.empty()) {
    now = d.front();
    now_h = now.first;
    now_w = now.second;
    d.pop_front();

    rep(i, 4) {
      next_h = now_h + dh[i];
      next_w = now_w + dw[i];

      if (next_h < 0 || next_h >= H || next_w < 0 || next_w >= W ||
          S[next_h][next_w] == '#' ||
          cost[next_h][next_w] <= cost[now_h][now_w]) {
        continue;
      }
      cost[next_h][next_w] = cost[now_h][now_w];
      d.push_front({next_h, next_w});
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        next_h = now_h + i;
        next_w = now_w + j;

        if (next_h < 0 || next_h >= H || next_w < 0 || next_w >= W ||
            S[next_h][next_w] == '#' ||
            cost[next_h][next_w] <= cost[now_h][now_w] + 1) {
          continue;
        }
        cost[next_h][next_w] = cost[now_h][now_w] + 1;
        d.push_back({next_h, next_w});
      }
    }
  }

  // rep(i,H){
  //     rep(j,W) cout << cost[i][j] << '\\t';
  //     cout << endl;
  // }

  if (cost[Dh][Dw] == inf)
    cost[Dh][Dw] = -1;
  cout << cost[Dh][Dw] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  // Failed to predict input format
  int 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];

  int inf = 1e5;

  deque<pair<int, int>> d;
  vector<vector<int>> visited(H, vector<int>(W, inf));
  vector<vector<bool>> check(H, vector<bool>(W, false));

  d.push_front(make_pair(Ch, Cw));
  visited[Ch][Cw] = 0;

  while (!d.empty()) {
    pair<int, int> now = d.front();
    d.pop_front();

    check[now.first][now.second] = true;

    vector<pair<int, int>> nexts(25);
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        if (0 <= now.first + i && now.first + i < H && 0 <= now.second + j &&
            now.second + j < W && S[now.first + i][now.second + j] == '.') {
          nexts.push_back(make_pair(now.first + i, now.second + j));
        }
      }
    }

    for (pair<int, int> next : nexts) {
      int ne = visited[next.first][next.second];
      int no = visited[now.first][now.second];

      if (abs(now.first - next.first) + abs(now.second - next.second) == 1) {
        if (ne > no)
          visited[next.first][next.second] = no;
        if (!check[next.first][next.second])
          d.push_front(next);
      } else {
        if (ne > no + 1)
          visited[next.first][next.second] = no + 1;
        if (!check[next.first][next.second])
          d.push_back(next);
      }
    }
  }

  int ans = visited[Dh][Dw];
  if (ans == inf)
    ans = -1;
  cout << ans << endl;

  return 0;
}