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

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

int main() {

  ll H, W;
  cin >> H >> W;

  int ch, cw, dh, dw;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  vector<string> S(H);
  vector<vector<bool>> M(H, vector<bool>(W, false));
  vector<vector<bool>> V(H, vector<bool>(W, false));
  vector<vector<int>> D(H, vector<int>(W, 1e9));
  rep(i, H) { cin >> S[i]; }
  rep(i, H) rep(j, W) if (S[i][j] == '#') V[i][j] = false;

  queue<pair<int, int>> q;
  q.push(make_pair(ch, cw));
  queue<pair<int, int>> qq;

  M[ch][cw] = true;
  V[ch][cw] = true;
  D[ch][cw] = 0;

  while (!q.empty() || !qq.empty()) {
    pair<int, int> p;
    if (!q.empty()) {
      p = q.front();
      q.pop();
    } else {
      p = qq.front();
      qq.pop();
    }

    int h = p.first;
    int w = p.second;
    M[h][w] = false;

    FOR(i, h - 2, h + 3) {
      FOR(j, w - 2, w + 3) {
        if (i >= 0 && i < H && j >= 0 && j < W && S[i][j] == '.') {
          if (i == h && j == w)
            continue;
          if (((i == h - 1 && j == w) || (i == h + 1 && j == w) ||
               (i == h && j == w - 1) || (i == h && j == w + 1))) {
            if (D[i][j] > D[h][w] && !M[i][j]) {
              q.push(make_pair(i, j));
              M[i][j] = true;
            }
            D[i][j] = min(D[i][j], D[h][w]);
          } else {
            if (D[i][j] > D[h][w] + 1 && !M[i][j]) {
              qq.push(make_pair(i, j));
              M[i][j] = true;
            }
            D[i][j] = min(D[i][j], D[h][w] + 1);
          }
        }
      }
    }
  }

  if (D[dh][dw] == 1e9)
    cout << -1 << endl;
  else
    cout << D[dh][dw] << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {

  ll H, W;
  cin >> H >> W;

  int ch, cw, dh, dw;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  vector<string> S(H);
  vector<vector<bool>> M(H, vector<bool>(W, false));
  vector<vector<int>> D(H, vector<int>(W, 1e9));
  rep(i, H) { cin >> S[i]; }
  // rep(i, H) rep(j, W) if(S[i][j] == '#') M[i][j] = false;

  queue<pair<int, int>> q;
  // stack<pair<int, int>> q;
  q.push(make_pair(ch, cw));
  M[ch][cw] = true;
  D[ch][cw] = 0;

  while (!q.empty()) {
    pair<int, int> p = q.front();
    // pair<int, int> p = q.top();
    q.pop();
    int h = p.first;
    int w = p.second;
    M[h][w] = false;

    FOR(i, h - 2, h + 3) {
      FOR(j, w - 2, w + 3) {
        if (i >= 0 && i < H && j >= 0 && j < W && S[i][j] == '.') {
          if (i == h && j == w)
            continue;
          if (((i == h - 1 && j == w) || (i == h + 1 && j == w) ||
               (i == h && j == w - 1) || (i == h && j == w + 1))) {
            if (D[i][j] > D[h][w] && !M[i][j]) {
              q.push(make_pair(i, j));
              M[i][j] = true;
            }
            D[i][j] = min(D[i][j], D[h][w]);
          } else {
            if (D[i][j] > D[h][w] + 1 && !M[i][j]) {
              q.push(make_pair(i, j));
              M[i][j] = true;
            }
            D[i][j] = min(D[i][j], D[h][w] + 1);
          }
        }
      }
    }
  }

  if (D[dh][dw] == 1e9)
    cout << -1 << endl;
  else
    cout << D[dh][dw] << endl;

  return 0;
}