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

#include <bits/stdc++.h>
using namespace std;
int h, w, inf = 1e9;
int main() {
  int ch, cw, dh, dw, i, j, k, l, y, x, cst = 0;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  vector<vector<int>> cost(h, vector<int>(w));
  for (i = 0; i < h; i++)
    cin >> s.at(i);
  for (i = 0; i < h; i++)
    for (j = 0; j < w; j++)
      cost.at(i).at(j) = inf;
  queue<pair<int, int>> q, qq;
  cost.at(ch).at(cw) = cst;
  q.push(make_pair(ch, cw));
  qq.push(make_pair(ch, cw));
  while (!q.empty()) {
    while (!q.empty()) {
      auto pr = q.front();
      y = pr.first;
      x = pr.second;
      q.pop();
      qq.push(make_pair(y, x));
      if (x - 1 >= 0 && s.at(y).at(x - 1) == '.' &&
          cost.at(y).at(x - 1) == inf) {
        cost.at(y).at(x - 1) = cst;
        q.push(make_pair(y, x - 1));
        qq.push(make_pair(y, x - 1));
      }
      if (x + 1 < w && s.at(y).at(x + 1) == '.' &&
          cost.at(y).at(x + 1) == inf) {
        cost.at(y).at(x + 1) = cst;
        q.push(make_pair(y, x + 1));
        qq.push(make_pair(y, x + 1));
      }
      if (y - 1 >= 0 && s.at(y - 1).at(x) == '.' &&
          cost.at(y - 1).at(x) == inf) {
        cost.at(y - 1).at(x) = cst;
        q.push(make_pair(y - 1, x));
        qq.push(make_pair(y - 1, x));
      }
      if (y + 1 < h && s.at(y + 1).at(x) == '.' &&
          cost.at(y + 1).at(x) == inf) {
        cost.at(y + 1).at(x) = cst;
        q.push(make_pair(y + 1, x));
        qq.push(make_pair(y + 1, x));
      }
    }
    if (cost.at(dh).at(dw) < inf) {
      cout << cost.at(dh).at(dw) << endl;
      return 0;
    }
    while (!qq.empty()) {
      auto pr = qq.front();
      y = pr.first;
      x = pr.second;
      qq.pop();
      for (k = -2; k <= 2; k++) {
        for (l = -2; l <= 2; l++) {
          if (0 <= y + k && y + k < h && 0 <= x + l && x + l < w &&
              s.at(y + k).at(x + l) == '.' && cost.at(y + k).at(x + l) == inf) {
            cost.at(y + k).at(x + l) = cst + 1;
            q.push(make_pair(y + k, x + l));
          }
        }
      }
    }
    cst++;
  }
  cout << -1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int h, w, inf = 1e9;
int main() {
  int ch, cw, dh, dw, i, j, k, l, y, x, cst = 0;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  vector<vector<int>> cost(h, vector<int>(w));
  for (i = 0; i < h; i++)
    cin >> s.at(i);
  for (i = 0; i < h; i++)
    for (j = 0; j < w; j++)
      cost.at(i).at(j) = inf;
  queue<pair<int, int>> q;
  q.push(make_pair(ch, cw));
  while (!q.empty()) {
    while (!q.empty()) {
      auto pr = q.front();
      y = pr.first;
      x = pr.second;
      q.pop();
      cost.at(y).at(x) = cst;
      if (x - 1 >= 0 && s.at(y).at(x - 1) == '.' && cost.at(y).at(x - 1) == inf)
        q.push(make_pair(y, x - 1));
      if (x + 1 < w && s.at(y).at(x + 1) == '.' && cost.at(y).at(x + 1) == inf)
        q.push(make_pair(y, x + 1));
      if (y - 1 >= 0 && s.at(y - 1).at(x) == '.' && cost.at(y - 1).at(x) == inf)
        q.push(make_pair(y - 1, x));
      if (y + 1 < h && s.at(y + 1).at(x) == '.' && cost.at(y + 1).at(x) == inf)
        q.push(make_pair(y + 1, x));
    }
    if (cost.at(dh).at(dw) < inf) {
      cout << cost.at(dh).at(dw) << endl;
      return 0;
    }
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        if (s.at(i).at(j) == '.' && cost.at(i).at(j) == inf) {
          for (k = -2; k <= 2; k++) {
            for (l = -2; l <= 2; l++) {
              if (0 <= i + k && i + k < h && 0 <= j + l && j + l < w &&
                  cost.at(i + k).at(j + l) == cst) {
                q.push(make_pair(i, j));
                k = 2;
                l = 2;
              }
            }
          }
        }
      }
    }
    cst++;
  }
  cout << -1 << endl;
}