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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;

#ifdef _DEBUG
inline void dump() { cerr << endl; }
template <typename Head> void dump(Head &&head) {
  cerr << head;
  dump();
}
template <typename Head, typename... Tail>
void dump(Head &&head, Tail &&...tail) {
  cerr << head << ", ";
  dump(forward<Tail>(tail)...);
}
#define debug(...)                                                             \\
  do {                                                                         \\
    cerr << __LINE__ << ":\\t" << #__VA_ARGS__ << " = ";                        \\
    dump(__VA_ARGS__);                                                         \\
  } while (false)
#else
#define dump(...)
#define debug(...)
#endif

template <typename T> struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

/////////////////////////////////////////////////////////////////////

const ll inf = 1LL << 60;

typedef pair<int, int> ii;

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

  vector<vector<ll>> d(h, vector<ll>(w, inf));
  d[ch][cw] = 0;

  queue<ii> que, que2;
  que.push(ii{ch, cw});
  que2.push(ii{ch, cw});

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

  while (!que.empty()) {
    ii p = que.front();
    que.pop();
    int gy = p.first;
    int gx = p.second;
    while (!que2.empty()) {
      ii t = que2.front();
      que2.pop();
      int ny = t.first;
      int nx = t.second;
      int wall = 0;
      for (int i = 0; i < 4; i++) {
        int tx = nx + dx[i];
        int ty = ny + dy[i];
        if (tx < 0 || w <= tx || ty < 0 || h <= ty)
          continue;
        if (s[ty][tx] == '#') {
          wall++;
          continue;
        }
        if (d[ty][tx] <= d[ny][nx])
          continue;
        d[ty][tx] = d[ny][nx];
        que2.push(ii{ty, tx});
      }
      if (wall == 0)
        continue;
      que.push(ii{ny, nx});
    }
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int tx = gx + j;
        int ty = gy + i;
        if (tx < 0 || w <= tx || ty < 0 || h <= ty)
          continue;
        if (s[ty][tx] == '#')
          continue;
        if (abs(i) + abs(j) <= 1)
          continue;
        if (d[ty][tx] <= d[gy][gx] + 1)
          continue;
        d[ty][tx] = d[gy][gx] + 1;
        que.push(ii{ty, tx});
        que2.push(ii{ty, tx});
      }
  }

  if (d[dh][dw] == inf)
    cout << -1 << endl;
  else
    cout << d[dh][dw] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;

#ifdef _DEBUG
inline void dump() { cerr << endl; }
template <typename Head> void dump(Head &&head) {
  cerr << head;
  dump();
}
template <typename Head, typename... Tail>
void dump(Head &&head, Tail &&...tail) {
  cerr << head << ", ";
  dump(forward<Tail>(tail)...);
}
#define debug(...)                                                             \\
  do {                                                                         \\
    cerr << __LINE__ << ":\\t" << #__VA_ARGS__ << " = ";                        \\
    dump(__VA_ARGS__);                                                         \\
  } while (false)
#else
#define dump(...)
#define debug(...)
#endif

template <typename T> struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

/////////////////////////////////////////////////////////////////////

const ll inf = 1LL << 60;

typedef pair<int, int> ii;

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

  vector<vector<ll>> d(h, vector<ll>(w, inf));
  d[ch][cw] = 0;

  queue<ii> que;
  que.push(ii{ch, cw});

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

  while (!que.empty()) {
    ii p = que.front();
    que.pop();
    int ny = p.first;
    int nx = p.second;
    int wall = 0;
    for (int i = 0; i < 4; i++) {
      int tx = nx + dx[i];
      int ty = ny + dy[i];
      if (tx < 0 || w <= tx || ty < 0 || h <= ty)
        continue;
      if (s[ty][tx] == '#') {
        wall++;
        continue;
      }
      if (d[ty][tx] <= d[ny][nx])
        continue;
      d[ty][tx] = d[ny][nx];
      que.push(ii{ty, tx});
    }
    if (wall == 0)
      continue;
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int tx = nx + j;
        int ty = ny + i;
        if (tx < 0 || w <= tx || ty < 0 || h <= ty)
          continue;
        if (s[ty][tx] == '#')
          continue;
        if (abs(i) + abs(j) <= 1)
          continue;
        if (d[ty][tx] <= d[ny][nx] + 1)
          continue;
        d[ty][tx] = d[ny][nx] + 1;
        que.push(ii{ty, tx});
      }
  }

  if (d[dh][dw] == inf)
    cout << -1 << endl;
  else
    cout << d[dh][dw] << endl;

  return 0;
}