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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <x86intrin.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define V vector
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
V<int> dx = {-1, 1, 0, 0};
V<int> dy = {0, 0, -1, 1};

char c[1010][1010];
int d[1010][1010];
const int INF = 1e9;

int main() {
  int h, w;
  cin >> h >> w;
  int sy, sx;
  cin >> sy >> sx;
  sy--;
  sx--;
  int gy, gx;
  cin >> gy >> gx;
  gy--;
  gx--;
  rep(i, h) rep(j, w) cin >> c[i][j];
  rep(i, h) rep(j, w) d[i][j] = INF;
  d[sy][sx] = 0;
  queue<P> que, nxt;
  que.push({sx, sy});

  int now = 0;
  bool flag = true;
  while (flag) {
    flag = false;
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      int x = p.first, y = p.second;
      nxt.push({x, y});
      rep(i, 4) {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx < 0 || nx >= w || ny < 0 || ny >= h)
          continue;
        if (c[ny][nx] == '#')
          continue;
        if (d[ny][nx] != INF)
          continue;
        d[ny][nx] = d[y][x];
        que.push({nx, ny});
        flag = true;
      }
    }
    while (!nxt.empty()) {
      P p = nxt.front();
      nxt.pop();
      int x = p.first, y = p.second;
      for (int dx = -2; dx <= 2; dx++) {
        for (int dy = -2; dy <= 2; dy++) {
          if (dx == 0 && dy == 0)
            continue;
          int nx = x + dx, ny = y + dy;
          if (nx < 0 || nx >= w || ny < 0 || ny >= h)
            continue;
          if (c[ny][nx] == '#')
            continue;
          if (d[ny][nx] != INF)
            continue;
          d[ny][nx] = d[y][x] + 1;
          que.push({nx, ny});
          flag = true;
        }
      }
    }
    now++;
  }
  if (d[gy][gx] != INF)
    cout << d[gy][gx] << endl;
  else
    cout << -1 << endl;

  /*
  rep(i, h) {
      rep(j, w) {
          if (d[i][j] == INF) cout << 'a' <<  " ";
          else cout << d[i][j] << " ";
      }
      cout << endl;
  }
  cout << now << endl;
  */

  return 0;
}

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <x86intrin.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define V vector
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
V<int> dx = {-1, 1, 0, 0};
V<int> dy = {0, 0, -1, 1};

char c[1010][1010];
const int INF = 1e9;

int main() {
  int h, w;
  cin >> h >> w;
  int sy, sx;
  cin >> sy >> sx;
  sy--;
  sx--;
  int gy, gx;
  cin >> gy >> gx;
  gy--;
  gx--;
  rep(i, h) rep(j, w) cin >> c[i][j];
  V<V<int>> d(h, V<int>(w, INF));
  d[sy][sx] = 0;
  queue<P> que;
  que.push({sx, sy});

  // V<V<bool>> saw(h, V<bool> (w, false));
  int now = 0;
  bool flag = true;
  while (flag) {
    flag = false;
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      int x = p.first, y = p.second;
      rep(i, 4) {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx < 0 || nx >= w || ny < 0 || ny >= h)
          continue;
        if (c[ny][nx] == '#')
          continue;
        if (d[ny][nx] != INF)
          continue;
        d[ny][nx] = d[y][x];
        que.push({nx, ny});
        flag = true;
      }
    }
    // cout << flag << endl;
    // int cnt = 0;
    rep(y, h) {
      rep(x, w) {
        if (d[y][x] == now) {
          que.push({x, y});
          // cnt++;
        }
      }
    }
    // cout << cnt << endl;
    int a = que.size();
    while (a--) {
      P p = que.front();
      que.pop();
      int x = p.first, y = p.second;
      for (int dx = -2; dx <= 2; dx++) {
        for (int dy = -2; dy <= 2; dy++) {
          if (dx == 0 && dy == 0)
            continue;
          int nx = x + dx, ny = y + dy;
          if (nx < 0 || nx >= w || ny < 0 || ny >= h)
            continue;
          if (c[ny][nx] == '#')
            continue;
          if (d[ny][nx] != INF)
            continue;
          d[ny][nx] = d[y][x] + 1;
          que.push({nx, ny});
          flag = true;
          // printf("nx=%d ny=%d\\n", nx+1,ny+1);
        }
      }
    }
    // cout << flag << endl;
    now++;
  }
  if (d[gy][gx] != INF)
    cout << d[gy][gx] << endl;
  else
    cout << -1 << endl;

  /*
  rep(i, h) {
      rep(j, w) {
          if (d[i][j] == INF) cout << 'a' <<  " ";
          else cout << d[i][j] << " ";
      }
      cout << endl;
  }
  cout << now << endl;
  */
  return 0;
}