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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
  int h, w;
  cin >> h >> w;
  pair<int, int> c, d;
  cin >> c.first >> c.second;
  cin >> d.first >> d.second;
  c.first--;
  d.first--;
  c.second--;
  d.second--;
  vector<string> s(h);
  for (int i = 0; i < h; i++)
    cin >> s[i];
  int dx[] = {1, -1, 0, 0};
  int dy[] = {0, 0, 1, -1};
  queue<pair<int, int>> q, q_w;
  vector<vector<int>> count(h, vector<int>(w, 1000000));
  q.push({c.first, c.second});
  count[c.first][c.second] = 0;
  bool goal = false;
  while (!q.empty() || !q_w.empty()) {
    pair<int, int> front;
    if (!q.empty()) {
      front = q.front();
      q.pop();
    } else {
      front = q_w.front();
      q_w.pop();
    }
    int ch = front.first;
    int cw = front.second;
    if (ch == d.first && cw == d.second) {
      goal = true;
      break;
    }
    for (int i = 0; i < 4; i++) {
      int nh = ch + dx[i];
      int nw = cw + dy[i];
      if (nh < 0 || h <= nh)
        continue;
      if (nw < 0 || w <= nw)
        continue;
      if (s[nh][nw] == '.' && count[nh][nw] > count[ch][cw]) {
        count[nh][nw] = count[ch][cw];
        q.push({nh, nw});
      }
    }
    for (int i = -2; i < 3; i++) {
      for (int k = -2; k < 3; k++) {
        int nh = ch + i;
        int nw = cw + k;
        if (nh < 0 || h <= nh)
          continue;
        if (nw < 0 || w <= nw)
          continue;
        if (s[nh][nw] == '.' && count[nh][nw] > count[ch][cw] + 1) {
          count[nh][nw] = count[ch][cw] + 1;
          q_w.push({nh, nw});
        }
      }
    }
  }
  if (goal)
    cout << count[d.first][d.second];
  else
    cout << -1;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
  int h, w;
  cin >> h >> w;
  pair<int, int> c, d;
  cin >> c.first >> c.second;
  cin >> d.first >> d.second;
  c.first--;
  d.first--;
  c.second--;
  d.second--;
  vector<string> s(h);
  for (int i = 0; i < h; i++)
    cin >> s[i];
  int ub = 1e6;
  int lb = -1;
  int dx[] = {1, -1, 0, 0};
  int dy[] = {0, 0, 1, -1};
  while (ub - lb > 1) {
    int mid = (ub + lb) / 2;
    queue<tuple<int, int, int, vector<string>>> q;
    vector<string> init = s;
    init[c.first][c.second] = '.';
    q.push({c.first, c.second, mid, init});
    bool reached = false;
    while (!q.empty()) {
      auto front = q.front();
      q.pop();
      int ch = get<0>(front);
      int cw = get<1>(front);
      int rest = get<2>(front);
      vector<string> mp = get<3>(front);
      if (ch == d.first && cw == d.second) {
        reached = true;
        break;
      }
      for (int i = 0; i < 4; i++) {
        int nh = ch + dx[i];
        int nw = cw + dy[i];
        if (nh < 0 || h <= nh)
          continue;
        if (nw < 0 || w <= nw)
          continue;
        if (mp[nh][nw] == '.') {
          vector<string> nxt = mp;
          nxt[nh][nw] = '-';
          q.push({nh, nw, rest, nxt});
        }
      }
      if (rest == 0)
        continue;
      for (int i = -2; i < 3; i++) {
        for (int k = -2; k < 3; k++) {
          int nh = ch + i;
          int nw = cw + k;
          if (nh < 0 || h <= nh)
            continue;
          if (nw < 0 || w <= nw)
            continue;
          if (mp[nh][nw] == '.') {
            vector<string> nxt = mp;
            nxt[nh][nw] = '-';
            q.push({nh, nw, rest - 1, nxt});
          }
        }
      }
    }
    if (reached)
      ub = mid;
    else
      lb = mid;
  }
  if (ub == 1e6)
    cout << -1;
  else
    cout << ub;
  return 0;
}