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

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

using ll = long long;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)

#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vs = vector<string>;
using vvs = vector<vs>;
using ld = long double;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, (int)v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

template <typename T, size_t S> void printArray(const T (&array)[S]) {
  for (auto val : array)
    std::cout << val << ", ";
  std::cout << "\\n";
}

const int mod = 1e9 + 7;
const int inf = 1e9 + 5;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << std::setprecision(10);

  int h, w;
  std::cin >> h >> w;
  int ch, cw;
  std::cin >> ch >> cw;
  ch--, cw--;
  int dh, dw;
  std::cin >> dh >> dw;
  dh--, dw--;
  vs grid(h);
  rep(i, h) std::cin >> grid[i];
  vvi cost(h, vi(w, 1e9));
  vvi visited(h, vi(w, 0));

  deque<pi> dq;
  cost[ch][cw] = 0;
  dq.emplace_back(ch, cw);
  while (dq.size()) {
    int y = dq.front().fi, x = dq.front().se;
    dq.pop_front();
    if (visited[y][x])
      continue;
    visited[y][x] = true;
    srep(dy, -2, 3) srep(dx, -2, 3) if (dy * dy + dx * dx != 0) {
      int ny = y + dy, nx = x + dx;
      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;
      if (grid[ny][nx] == '#')
        continue;
      bool isWarp = (dy * dy + dx * dx > 1);
      if (cost[ny][nx] <= cost[y][x] + isWarp)
        continue;
      cost[ny][nx] = cost[y][x] + isWarp;
      if (isWarp)
        dq.emplace_back(ny, nx);
      else
        dq.emplace_front(ny, nx);
    }
  }

  std::cout << (cost[dh][dw] != 1e9 ? cost[dh][dw] : -1) << "\\n";
}


Error: TLE

Fixed submission:

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

using ll = long long;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)

#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vs = vector<string>;
using vvs = vector<vs>;
using ld = long double;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, (int)v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

template <typename T, size_t S> void printArray(const T (&array)[S]) {
  for (auto val : array)
    std::cout << val << ", ";
  std::cout << "\\n";
}

const int mod = 1e9 + 7;
const int inf = 1e9 + 5;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << std::setprecision(10);

  int h, w;
  std::cin >> h >> w;
  int ch, cw;
  std::cin >> ch >> cw;
  ch--, cw--;
  int dh, dw;
  std::cin >> dh >> dw;
  dh--, dw--;
  vector<string> grid(h);
  rep(i, h) std::cin >> grid[i];

  vvi cost(h, vi(w, 1e9));
  cost[ch][cw] = 0;

  queue<pi> que, wall;
  que.emplace(ch, cw);
  while (que.size()) {
    while (que.size()) {
      int y = que.front().fi, x = que.front().se;
      que.pop();
      srep(dy, -1, 2) srep(dx, -1, 2) if (dy * dy + dx * dx != 0) {
        int ny = y + dy, nx = x + dx;
        if (ny < 0 || ny >= h || nx < 0 || nx >= w)
          continue;
        if (cost[ny][nx] < cost[y][x])
          continue;
        if (dy * dy + dx * dx == 2 && grid[ny][nx] == '.')
          cost[ny][nx] = cost[y][x] + 1, wall.emplace(ny, nx);
        else if (grid[ny][nx] == '#')
          cost[ny][nx] = cost[y][x] + 1, wall.emplace(ny, nx);
        else
          cost[ny][nx] = cost[y][x], que.emplace(ny, nx);
      }
    }
    while (wall.size()) {
      int y = wall.front().fi, x = wall.front().se;
      wall.pop();
      srep(dy, -1, 2) srep(dx, -1, 2) if (dy * dy + dx * dx != 0) {
        int ny = y + dy, nx = x + dx;
        if (ny < 0 || ny >= h || nx < 0 || nx >= w)
          continue;
        if (cost[ny][nx] != 1e9)
          continue;
        if (grid[ny][nx] == '#')
          continue;
        cost[ny][nx] = cost[y][x];
        que.emplace(ny, nx);
      }
    }
  }
  std::cout << (cost[dh][dw] != 1e9 ? cost[dh][dw] : -1) << "\\n";
}