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

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

#define int long long
using ll = long long;
using ld = long double;
using intpair = pair<int, int>;
using intmap = map<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
const int MOD = 1e9 + 7;
inline int mod(ll a, int m = MOD) { return (a % m + m) % m; }
inline bool bit(ll b, ll i) { return b & (1 << i); }
inline ll ceiv(ll a, ll b) { return (a + b - 1) / b; }
// struct Fast {Fast(){cin.tie(0);ios::sync_with_stdio(0);}} fast;

#define debug(x) cerr << #x << ": " << x << '\\n'
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()

int H, W, Ch, Cw, Dh, Dw;
vector<string> field;

inline bool is_safe(int h, int w) {
  return (0 <= w && w < W) && (0 <= h && h < H);
}

int dh[4] = {0, 1, 0, -1};
int dw[4] = {1, 0, -1, 0};

signed main() {
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;

  vvi cost(H, vi(W, INT_MAX));

  rep(i, H) {
    string s;
    cin >> s;
    field.push_back(s);
  }
  queue<intpair> q0, q1;
  q0.push({Ch, Cw});
  cost[Ch][Cw] = 0;

  while (!q0.empty() || !q1.empty()) {
    int h, w;
    if (!q0.empty()) {
      h = q0.front().first;
      w = q0.front().second;
      q0.pop();
    } else {
      h = q1.front().first;
      w = q1.front().second;
      q1.pop();
    }

    int nowcost = cost[h][w];

    if (h == Dh && w == Dw) {
      cout << nowcost << endl;
      exit(0);
    }

    rep(i, 4) {
      if (!is_safe(h + dh[i], w + dw[i]))
        continue;
      if (cost[h + dh[i]][w + dw[i]] <= nowcost)
        continue;
      if (field[h + dh[i]][w + dw[i]] == '#')
        continue;
      q0.push({h + dh[i], w + dw[i]});
      cost[h + dh[i]][w + dw[i]] = nowcost;
    }

    reps(dH, -2, 2) reps(dW, -2, 2) {
      if (!is_safe(h + dH, w + dW))
        continue;
      if (cost[h + dH][w + dW] <= nowcost + 1)
        continue;
      if (field[h + dH][w + dW] == '#')
        continue;
      q1.push({h + dH, w + dW});
      cost[h + dH][w + dW] = nowcost + 1;
    }
  }

  cout << -1 << endl;
}

Error: TLE

Fixed submission:

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

#define int long long
using ll = long long;
using ld = long double;
using intpair = pair<int, int>;
using intmap = map<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
const int MOD = 1e9 + 7;
inline int mod(ll a, int m = MOD) { return (a % m + m) % m; }
inline bool bit(ll b, ll i) { return b & (1 << i); }
inline ll ceiv(ll a, ll b) { return (a + b - 1) / b; }
// struct Fast {Fast(){cin.tie(0);ios::sync_with_stdio(0);}} fast;

#define debug(x) cerr << #x << ": " << x << '\\n'
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()

int H, W, Ch, Cw, Dh, Dw;
vector<string> field;
vector<vector<bool>> visited(1000, vector<bool>(1000, false));
vector<vector<int>> dist(1000, vector<int>(1000, INT_MAX));
vector<vector<int>> ufmap(1000, vector<int>(1000, INT_MAX));

inline bool is_safe(int x, int y) {
  return (0 <= x && x < W) && (0 <= y && y < H);
}

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
// int dx[8] = {-1,0,1,1,1,0,-1,-1};
// int dy[8] = {1,1,1,0,-1,-1,-1,0};

int ufcount = 0;
void dfs(int s, int t) {
  if (!is_safe(s, t))
    return;
  queue<pair<int, int>> q;
  q.push({s, t});
  while (!q.empty()) {
    auto [x, y] = q.front();
    ufmap[y][x] = ufcount;
    q.pop();
    rep(i, 4) {
      if (!is_safe(x + dx[i], y + dy[i]))
        continue;
      if (visited[y + dy[i]][x + dx[i]])
        continue;
      if (field[y + dy[i]][x + dx[i]] == '#')
        continue;
      dist[y + dy[i]][x + dx[i]] = dist[y][x];
      visited[y + dy[i]][x + dx[i]] = true;
      q.push({x + dx[i], y + dy[i]});
    }
  }
}

signed main() {
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;

  field = vector<string>(H);
  rep(i, H) cin >> field[i];

  rep(x, W) rep(y, H) {
    if (visited[y][x])
      continue;
    visited[y][x] = true;
    dfs(x, y);
    ufcount++;
  }

  queue<pair<int, int>> q;
  dist[Ch][Cw] = 0;
  q.push({Cw, Ch});

  while (!q.empty()) {
    auto [x, y] = q.front();
    q.pop();

    rep(i, 4) {
      if (!is_safe(x + dx[i], y + dy[i]))
        continue;
      if (dist[y][x] >= dist[y + dy[i]][x + dx[i]])
        continue;
      if (field[y + dy[i]][x + dx[i]] == '#')
        continue;
      dist[y + dy[i]][x + dx[i]] = dist[y][x];
      q.push({x + dx[i], y + dy[i]});
    }

    reps(Dx, x - 2, x + 2) reps(Dy, y - 2, y + 2) {
      if (!is_safe(Dx, Dy))
        continue;
      if (dist[y][x] >= dist[Dy][Dx])
        continue;
      if (field[Dy][Dx] == '#')
        continue;
      if (ufmap[Dy][Dx] == ufmap[y][x])
        continue;
      dist[Dy][Dx] = dist[y][x] + 1;
      q.push({Dx, Dy});
    }
  }

  if (dist[Dh][Dw] == INT_MAX) {
    cout << -1 << endl;
  } else {
    cout << dist[Dh][Dw] << endl;
  }
}