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

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

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> PLL;
typedef pair<P, P> PP;
typedef vector<ll> VECLL;
typedef vector<P> VECP;
typedef priority_queue<P, VECP, greater<P>> PQP;
typedef priority_queue<ll, VECLL, greater<ll>> PQll;

#define rep(i, a, n) for (ll i = a; i < n; i++)
#define rrep(i, a, n) for (ll i = n; i >= a; i--)
#define index_check(i, n)                                                      \\
  if (i > n - 1 || i < 0)                                                      \\
    continue;
#define LINF (ll)1e18
#define INF (int)1e9
#define fs first
#define sc second

int main() {
  ll sh, sw, gh, gw;
  ll h, w;

  cin >> h >> w;
  cin >> sh >> sw >> gh >> gw;
  char c[h][w];
  ll memo[h][w];
  bool is_visited[1000][1000];
  rep(i, 0, h) rep(j, 0, w) cin >> c[i][j];
  rep(i, 0, h) rep(j, 0, w) memo[i][j] = LINF;
  memo[sh - 1][sw - 1] = 0;

  is_visited[sh - 1][sw - 1] = true;
  deque<P> q;
  q.push_front(P(sh - 1, sw - 1));
  while (!q.empty()) {
    P v = q.front();
    q.pop_front();
    ll y = v.fs, x = v.sc;

    rep(k, -2, 3) {
      rep(l, -2, 3) {
        if (y + k < 0 || y + k >= h || x + l < 0 || x + l >= w)
          continue;
        if (c[y + k][x + l] == '#')
          continue;

        if (abs(k) + abs(l) == 1) {
          if (memo[y + k][x + l] > memo[y][x]) {
            memo[y + k][x + l] = memo[y][x];
            q.push_front(P(y + k, x + l));
          }
        } else {
          if (memo[y + k][x + l] > memo[y][x] + 1) {
            memo[y + k][x + l] = memo[y][x] + 1;
            q.push_back(P(y + k, x + l));
          }
        }
      }
    }
  }

  if (memo[gh - 1][gw - 1] != LINF)
    cout << memo[gh - 1][gw - 1] << endl;
  else
    cout << -1 << endl;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> PLL;
typedef pair<P, P> PP;
typedef vector<ll> VECLL;
typedef vector<P> VECP;
typedef priority_queue<P, VECP, greater<P>> PQP;
typedef priority_queue<ll, VECLL, greater<ll>> PQll;

#define rep(i, a, n) for (ll i = a; i < n; i++)
#define rrep(i, a, n) for (ll i = n; i >= a; i--)
#define index_check(i, n)                                                      \\
  if (i > n - 1 || i < 0)                                                      \\
    continue;
#define LINF (ll)1e18
#define INF (int)1e9
#define fs first
#define sc second
ll sh, sw, gh, gw;
ll h, w;
char c[1000][1000];
ll memo[1000][1000];
bool is_visited[1000][1000];

int main() {
  cin >> h >> w;
  cin >> sh >> sw >> gh >> gw;
  rep(i, 0, h) rep(j, 0, w) cin >> c[i][j];
  rep(i, 0, h) rep(j, 0, w) memo[i][j] = LINF;
  memo[sh - 1][sw - 1] = 0;

  is_visited[sh - 1][sw - 1] = true;
  queue<P> q;
  q.push(P(sh - 1, sw - 1));
  while (!q.empty()) {
    P v = q.front();
    q.pop();
    ll y = v.fs, x = v.sc;

    rep(k, -2, 3) {
      rep(l, -2, 3) {
        if (y + k < 0 || y + k >= h || x + l < 0 || x + l >= w)
          continue;
        if (c[y + k][x + l] == '#')
          continue;

        if (abs(k) + abs(l) == 1) {
          if (memo[y + k][x + l] > memo[y][x] /*&& !is_visited[y+k][x+l]*/) {
            memo[y + k][x + l] = memo[y][x];
            // is_visited[y+k][x+l] = true;
            q.push(P(y + k, x + l));
          }
        } else {
          if (memo[y + k][x + l] >
              memo[y][x] + 1 /*&& !is_visited[y+k][x+l]*/) {
            memo[y + k][x + l] = memo[y][x] + 1;
            q.push(P(y + k, x + l));
          }
        }
      }
    }
  }

  if (memo[gh - 1][gw - 1] != LINF)
    cout << memo[gh - 1][gw - 1] << endl;
  else
    cout << -1 << endl;
}