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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (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 ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
#define P pair<int, int>
#define optimize_cin()                                                         \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
using namespace std;
using ll = long long;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

static const ll INF = 1e15;

int h, w;

ll cost[1010][1010];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
  optimize_cin();
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  --ch;
  --cw;
  --dh;
  --dw;
  vector<string> v(h);
  rep(i, h) { cin >> v[i]; }

  vector<vector<ll>> dist(h, vector<ll>(w, INF));
  dist[ch][cw] = 0;
  queue<P> p;
  p.emplace(ch, cw);

  while (!p.empty()) {
    P lookin = p.front();
    p.pop();
    int nowx = lookin.first, nowy = lookin.second;

    queue<P> q;
    q.emplace(lookin);

    while (!q.empty()) {
      P now = q.front();
      q.pop();
      int x = now.first, y = now.second;
      ll nowdist = dist[x][y];

      rep(i, 4) {
        int newx = x + dx[i], newy = y + dy[i];
        if (newx < 0 || h <= newx || newy < 0 || w <= newy)
          continue;
        if (v[newx][newy] == '#')
          continue;
        if (dist[newx][newy] <= nowdist)
          continue;
        dist[newx][newy] = nowdist;
        q.emplace(newx, newy);
      }

      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          int newx = x + i, newy = y + j;
          if (newx < 0 || h <= newx || newy < 0 || w <= newy)
            continue;
          if (v[newx][newy] == '#')
            continue;
          if (dist[newx][newy] <= nowdist + 1)
            continue;
          p.emplace(newx, newy);
          dist[newx][newy] = nowdist + 1;
        }
      }
    }
  }

  if (dist[dh][dw] != INF)
    cout << dist[dh][dw] << endl;
  else
    cout << -1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (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 ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
#define P pair<int, int>
#define optimize_cin()                                                         \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
using namespace std;
using ll = long long;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

static const ll INF = 1e15;

int h, w;
vector<string> v;
ll cost[1010][1010];

int main() {
  optimize_cin();
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  --ch;
  --cw;
  --dh;
  --dw;
  v.resize(h);
  rep(i, h) { cin >> v[i]; }

  rep(i, h) rep(j, w) cost[i][j] = INF;
  cost[ch][cw] = 0;

  queue<P> q;
  q.push(mp(ch, cw));

  while (!q.empty()) {
    P now = q.front();
    q.pop();
    int x = now.first, y = now.second;

    v[x][y] = '#';

    rep(i, 5) {
      rep(j, 5) {
        if (x - 2 + i < 0 || x - 2 + i >= h || y - 2 + j < 0 ||
            y - 2 + j >= w || v[x - 2 + i][y - 2 + j] == '#')
          continue;
        q.push(mp(x - 2 + i, y - 2 + j));
        chmin(cost[x - 2 + i][y - 2 + j], cost[x][y] + 1);
      }
    }

    if (x - 1 >= 0 && v[x - 1][y] == '.') {
      chmin(cost[x - 1][y], cost[x][y]);
      q.push(mp(x - 1, y));
    }
    if (x + 1 <= h - 1 && v[x + 1][y] == '.') {
      chmin(cost[x + 1][y], cost[x][y]);
      q.push(mp(x + 1, y));
    }
    if (y - 1 >= 0 && v[x][y - 1] == '.') {
      chmin(cost[x][y - 1], cost[x][y]);
      q.push(mp(x, y - 1));
    }
    if (y + 1 <= w - 1 && v[x][y + 1] == '.') {
      chmin(cost[x][y + 1], cost[x][y]);
      q.push(mp(x, y + 1));
    }
  }

  if (cost[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << cost[dh][dw] << endl;
  return 0;
}