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

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define repi(i, x)                                                             \\
  for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++)
#define all(x) (x).begin(), (x).end()
#define input(array)                                                           \\
  repi(_itr, array) { cin >> *(_itr); }
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
string solve(bool a) { return ((a) ? "Yes" : "No"); }
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int, int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}
const long long INFLL = 1LL << 60;
const int INF = 1 << 30;
const double PI = acos(-1);
int main() {
  int h, w;
  cin >> h >> w;
  Pi at, ter;
  int ans = 0;
  cin >> at.F >> at.S >> ter.F >> ter.S;
  at.F--;
  at.S--;
  ter.F--;
  ter.S--;
  vector<string> data(h);
  input(data);
  VVi dis(h, Vi(w, INF));
  deque<Pi> q;
  q.push_back(at);
  dis[at.F][at.S] = 0;
  while (!q.empty()) {
    at = q.front();
    q.pop_front();
    for (int ni = max(0, at.F - 2); ni < min(h, at.F + 3); ni++) {
      for (int nj = max(0, at.S - 2); nj < min(w, at.S + 3); nj++) {
        if (data[ni][nj] == '#')
          continue;
        if (abs(at.F - ni) + abs(at.S - nj) == 1) {
          if (chmin(dis[ni][nj], dis[at.F][at.S])) {
            q.push_front(mp(ni, nj));
          }
        } else {
          if (chmin(dis[ni][nj], dis[at.F][at.S] + 1)) {
            q.push_back(mp(ni, nj));
          }
        }
      }
    }
  }

  ans = dis[ter.F][ter.S];
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  //	cout<<ans<<endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define repi(i, x)                                                             \\
  for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++)
#define all(x) (x).begin(), (x).end()
#define input(array)                                                           \\
  repi(_itr, array) { cin >> *(_itr); }
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
string solve(bool a) { return ((a) ? "Yes" : "No"); }
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int, int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}
const long long INFLL = 1LL << 60;
const int INF = 1 << 30;
const double PI = acos(-1);
int main() {
  int h, w;
  int ans = 0;
  cin >> h >> w;
  pair<int, int> at, ter;
  cin >> at.F >> at.S >> ter.F >> ter.S;
  at.F--;
  at.S--;
  ter.F--;
  ter.S--;
  vector<string> data(h);
  input(data);
  vector<vector<Pi>> dis(h, vector<Pi>(w, mp(INF, INF)));
  queue<Pi> q;
  q.push(at);
  dis[at.F][at.S] = mp(0, 1);
  //	clog << "!";
  while (!q.empty()) {
    at = q.front();
    q.pop();
    /*
                    for (int ni = max(0, at.F - 1); ni < min(h, at.F + 2);ni++){
                            for (int nj = max(0, at.S - 1); nj < min(w, at.S +
       2); nj++) { if((ni==at.F)xor(nj==at.S) and data[ni][nj]=='.'){
                                            if(chmin(dis[ni][nj],dis[at.F][at.S])){
                                                    q.push(mp(ni, nj));
                                            }
                                    }
                            }
                    }
    */
    for (int ni = max(0, at.F - 2); ni < min(h, at.F + 3); ni++) {
      for (int nj = max(0, at.S - 2); nj < min(w, at.S + 3); nj++) {
        if (ni == at.F and nj == at.S)
          continue;
        if (dis[ni][nj].S == 1)
          continue;
        if (data[ni][nj] == '.') {

          if (abs(ni - at.F) + abs(nj - at.S) == 1) {
            if (chmin(dis[ni][nj], dis[at.F][at.S])) {
              q.push(mp(ni, nj));
            }
          } else {
            if ((dis[ni][nj].F > dis[at.F][at.S].F + 1)) {
              q.push(mp(ni, nj));
              dis[ni][nj] = mp(dis[at.F][at.S].F + 1, 2);
            }
          }
        }
      }
    }
  }
  if (dis[ter.F][ter.S].F == INF) {
    cout << -1 << endl;
    return 0;
  }
  /*

  rep(i,h){
          rep(j, w) { cout << dis[i][j] << " "; }
          clog << endl;
      }
          */
  cout << dis[ter.F][ter.S].F << endl;
}