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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

bool inside(ll h, ll w, ll y, ll x) {
  return (0 <= y && y < h && 0 <= x && x < w);
}

int main() {
  ll h, w;
  cin >> h >> w;

  ll ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;

  ll dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;

  vector<string> g(h);
  for (int i = 0; i < h; i++) {
    cin >> g[i];
  }

  queue<pair<int, int>> que;
  queue<pair<int, int>> warp;
  vector<vector<int>> dist(h, vector<int>(w, -1));

  que.push(make_pair(ch, cw));
  dist[ch][cw] = 0;

  vector<int> movey = {-1, 0, 1, 0};
  vector<int> movex = {0, 1, 0, -1};

  while (!que.empty() || !warp.empty()) {
    ll y, x;
    if (que.empty()) {
      tie(y, x) = warp.front();
      warp.pop();
    } else {
      tie(y, x) = que.front();
      que.pop();
    }

    for (int i = 0; i < 4; i++) {
      ll ny = y + movey[i];
      ll nx = x + movex[i];
      if (inside(h, w, ny, nx) && g[ny][nx] == '.') {
        if (dist[ny][nx] == -1) {
          que.push(make_pair(ny, nx));
          dist[ny][nx] = dist[y][x];
        } else {
          if (dist[y][x] < dist[ny][nx]) {
            que.push(make_pair(ny, nx));
            dist[ny][nx] = dist[y][x];
          }
        }
      }
    }

    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        ll ny = y + 2 - i;
        ll nx = x + 2 - j;
        if (inside(h, w, ny, nx) && g[ny][nx] == '.') {
          if (dist[ny][nx] == -1 || dist[y][x] + 1 < dist[ny][nx]) {
            warp.push(make_pair(ny, nx));
            dist[ny][nx] = dist[y][x] + 1;
          }
        }
      }
    }
  }

  cout << dist[dh][dw] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

bool inside(ll h, ll w, ll y, ll x) {
  return (0 <= y && y < h && 0 <= x && x < w);
}

int main() {
  ll h, w;
  cin >> h >> w;

  ll ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;

  ll dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;

  vector<string> g(h);
  for (int i = 0; i < h; i++) {
    cin >> g[i];
  }

  queue<pair<int, int>> que;
  vector<vector<int>> dist(h, vector<int>(w, -1));

  que.push(make_pair(ch, cw));
  dist[ch][cw] = 0;

  vector<int> movey = {-1, 0, 1, 0};
  vector<int> movex = {0, 1, 0, -1};

  while (!que.empty()) {
    ll y, x;
    tie(y, x) = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      ll ny = y + movey[i];
      ll nx = x + movex[i];
      if (inside(h, w, ny, nx) && g[ny][nx] == '.') {
        if (dist[ny][nx] == -1) {
          que.push(make_pair(ny, nx));
          dist[ny][nx] = dist[y][x];
        } else {
          if (dist[y][x] < dist[ny][nx]) {
            que.push(make_pair(ny, nx));
            dist[ny][nx] = dist[y][x];
          }
        }
      }
    }

    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        ll ny = y + 2 - i;
        ll nx = x + 2 - j;
        if (inside(h, w, ny, nx) && g[ny][nx] == '.') {
          if (dist[ny][nx] == -1 || dist[y][x] + 1 < dist[ny][nx]) {
            que.push(make_pair(ny, nx));
            dist[ny][nx] = dist[y][x] + 1;
          }
        }
      }
    }
  }

  cout << dist[dh][dw] << endl;
}