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

#include "bits/stdc++.h"
using namespace std;
typedef long long li;

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

  li h, w;
  cin >> h >> w;
  li ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;

  vector<string> field(h + 2, string(w + 2, '#'));
  li hh = h + 2;
  li ww = w + 2;

  for (int i = 0; i < h; ++i) {
    string line;
    cin >> line;
    for (int j = 0; j < w; ++j) {
      field[i + 1][j + 1] = line[j];
    }
  }

  const li inf = 1LL << 50;
  vector<vector<li>> dists(hh, vector<li>(ww, inf));
  deque<tuple<li, li, li>> q;
  q.emplace_back(ch, cw, 0);
  dists[ch][cw] = 0;

  while (not q.empty()) {
    const auto pos = q.front();
    q.pop_front();
    li x = get<0>(pos);
    li y = get<1>(pos);
    li dist = get<2>(pos);
    if (dists[x][y] < dist) {
      continue;
    }

    for (int dx = -2; dx <= 2; ++dx) {
      for (int dy = -2; dy <= 2; ++dy) {
        if (dx == 0 && dy == 0) {
          continue;
        }
        li nx = x + dx;
        li ny = y + dy;
        if (nx < 0 || nx >= hh || ny < 0 || ny >= ww) {
          continue;
        }
        if (field[nx][ny] == '#') {
          continue;
        }
        li cost;
        if (abs(dx) + abs(dy) == 1) {
          cost = 0;
        } else {
          cost = 1;
        }
        li ndist = dist + cost;
        if (dists[nx][ny] <= ndist) {
          continue;
        }
        if (cost == 0) {
          q.emplace_front(nx, ny, ndist);
        } else {
          q.emplace_back(nx, ny, ndist);
        }
        dists[nx][ny] = ndist;
      }
    }
  }
  li ans = dists[dh][dw];
  if (ans == inf) {
    ans = -1;
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long li;

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

  li h, w;
  cin >> h >> w;
  li ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;

  vector<string> field(h + 2, string(w + 2, '#'));
  li hh = h + 2;
  li ww = w + 2;

  for (int i = 0; i < h; ++i) {
    string line;
    cin >> line;
    for (int j = 0; j < w; ++j) {
      field[i + 1][j + 1] = line[j];
    }
  }

  const li inf = 1LL << 50;
  vector<vector<li>> dists(hh, vector<li>(ww, inf));
  queue<tuple<li, li, li>> q;
  q.emplace(ch, cw, 0);
  dists[ch][cw] = 0;

  while (not q.empty()) {
    const auto pos = q.front();
    q.pop();
    li x = get<0>(pos);
    li y = get<1>(pos);
    li dist = get<2>(pos);
    if (dists[x][y] < dist) {
      continue;
    }

    for (int dx = -2; dx <= 2; ++dx) {
      for (int dy = -2; dy <= 2; ++dy) {
        if (dx == 0 && dy == 0) {
          continue;
        }
        li nx = x + dx;
        li ny = y + dy;
        if (nx < 0 || nx >= hh || ny < 0 || ny >= ww) {
          continue;
        }
        if (field[nx][ny] == '#') {
          continue;
        }
        li cost;
        if (abs(dx) + abs(dy) == 1) {
          cost = 0;
        } else {
          cost = 1;
        }
        li ndist = dist + cost;
        if (dists[nx][ny] <= ndist) {
          continue;
        }
        q.emplace(nx, ny, ndist);
        dists[nx][ny] = ndist;
      }
    }
  }
  li ans = dists[dh][dw];
  if (ans == inf) {
    ans = -1;
  }
  cout << ans << endl;

  return 0;
}