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

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

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

int R, C;

string s[1001];

int memo[1001][1001];

struct State {
  int first, second, cost;
};

int main2() {
  R = nextLong();
  C = nextLong();

  int sr = nextLong() - 1;
  int sc = nextLong() - 1;
  int gr = nextLong() - 1;
  int gc = nextLong() - 1;

  REP(i, R) { cin >> s[i]; }

  const int INF = 1001001001;
  REP(i, 1001) REP(j, 1001) memo[i][j] = INF;

  deque<State> q;
  q.push_front((State){sr, sc, 0});
  memo[sr][sc] = 0;

  while (!q.empty()) {
    auto cur = q.front();
    q.pop_front();

    if (memo[cur.first][cur.second] != cur.cost)
      continue;

    for (int dr = -2; dr <= 2; dr++) {
      for (int dc = -2; dc <= 2; dc++) {
        int nr = cur.first + dr;
        int nc = cur.second + dc;
        if (!(0 <= nr && nr < R && 0 <= nc && nc < C))
          continue;
        if (s[nr][nc] != '.')
          continue;

        if (abs(dr) + abs(dc) == 1) {
          if (memo[nr][nc] > cur.cost + 0) {
            memo[nr][nc] = cur.cost + 0;
            q.push_front({nr, nc, memo[nr][nc]});
          }
        } else {
          if (memo[nr][nc] > cur.cost + 1) {
            memo[nr][nc] = cur.cost + 1;
            q.push_back({nr, nc, memo[nr][nc]});
          }
        }
      }
    }
  }
  ll ans = memo[gr][gc];
  if (ans == INF) {
    ans = -1;
  }
  cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

int R, C;

string s[1001];

int memo[1001][1001];

int main2() {
  R = nextLong();
  C = nextLong();

  int sr = nextLong() - 1;
  int sc = nextLong() - 1;
  int gr = nextLong() - 1;
  int gc = nextLong() - 1;

  REP(i, R) { cin >> s[i]; }

  const int INF = 1001001001;
  REP(i, 1001) REP(j, 1001) memo[i][j] = INF;

  deque<pair<int, int>> q;
  q.push_front({sr, sc});
  memo[sr][sc] = 0;

  while (!q.empty()) {
    auto cur = q.front();
    q.pop_front();

    for (int dr = -2; dr <= 2; dr++) {
      for (int dc = -2; dc <= 2; dc++) {
        int nr = cur.first + dr;
        int nc = cur.second + dc;
        if (!(0 <= nr && nr < R && 0 <= nc && nc < C))
          continue;
        if (s[nr][nc] != '.')
          continue;

        if (abs(dr) + abs(dc) == 1) {
          if (memo[nr][nc] > memo[cur.first][cur.second] + 0) {
            memo[nr][nc] = memo[cur.first][cur.second] + 0;
            q.push_front({nr, nc});
          }
        } else {
          if (memo[nr][nc] > memo[cur.first][cur.second] + 1) {
            memo[nr][nc] = memo[cur.first][cur.second] + 1;
            q.push_front({nr, nc});
          }
        }
      }
    }
  }
  ll ans = memo[gr][gc];
  if (ans == INF) {
    ans = -1;
  }
  cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}