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

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#define dlog(str) cout << "====" << str << endl;
#else
#define dlog(str)
#endif
#define INF (99999999)
#define MOD 1000000007
#define REP(i, n) for (int i = 0, i##_l = (n); i < i##_l; i++)
#define FOR(i, s, e) for (int i = s, i##_l = (e); i < i##_l; i++)
#define LLI long long int
#define _min(a, b) ((a < b) ? a : b)
#define _max(a, b) ((a < b) ? b : a)
#define chmax(a, b) a = _max(a, b)
#define chmin(a, b) a = _min(a, b)
#define bit(a, shift) ((a>>shift)&1))
#define pm(a) ((a) ? 1 : -1)
#define SORT(v) sort(v.begin(), v.end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
// int 2.14E±9    lli 9.2E±18    double 1.7E±380

template <typename T> struct vec2 {
  T x, y;
  vec2(T _x, T _y) : x(_x), y(_y) {}
};
#define veci2 vec2<int>

int d4x[4] = {1, 0, -1, 0};
int d4y[4] = {0, 1, 0, -1};

int main() {
  cout << fixed << setprecision(10);

  int h, w;
  cin >> h >> w;

  int cx, cy;
  cin >> cy >> cx;
  cx--;
  cy--;

  int dx, dy;
  cin >> dy >> dx;
  dx--;
  dy--;

  vector<string> s(h);
  REP(i, h) { cin >> s[i]; }

  vector<vector<int>> cost(h, vector<int>(w, INF));

  queue<veci2> q;
  queue<veci2> q2;
  q.push({cx, cy});
  cost[cy][cx] = 0;

  while (q.empty() == false) {
    while (q.empty() == false) {
      auto pos = q.front();
      q.pop();
      q2.push(pos);

      REP(i, 4) {
        veci2 next = pos;
        next.x += d4x[i];
        next.y += d4y[i];

        if (next.x < 0 || w <= next.x)
          continue;
        if (next.y < 0 || h <= next.y)
          continue;

        if (s[next.y][next.x] == '#')
          continue;

        if (cost[pos.y][pos.x] < cost[next.y][next.x]) {
          cost[next.y][next.x] = cost[pos.y][pos.x];
          q.push(next);
        }
      }
    }
    while (q2.empty() == false) {
      auto pos = q2.front();
      q2.pop();

      FOR(x, -2, 3) {
        FOR(y, -2, 3) {
          veci2 next = pos;
          next.x += x;
          next.y += y;

          if (next.x < 0 || w <= next.x)
            continue;
          if (next.y < 0 || h <= next.y)
            continue;

          if (s[next.y][next.x] == '#')
            continue;

          if (cost[pos.y][pos.x] + 1 < cost[next.y][next.x]) {
            cost[next.y][next.x] = cost[pos.y][pos.x] + 1;
            q.push(next);
          }
        }
      }
    }
  }

  if (cost[dy][dx] == INF) {
    cout << -1;
    return 0;
  }
  cout << cost[dy][dx];

  return 0;
}

// FOR(x, -2, 3)
//{
//     FOR(y, -2, 3)
//     {
//         veci2 next = pos;
//         next.x += x;
//         next.y += y;
//
//         if (next.x < 0 || w <= next.x)continue;
//         if (next.y < 0 || h <= next.y)continue;
//
//         if (s[next.y][next.x] == '#')continue;
//
//         if (cost[pos.y][pos.x] + 1 < cost[next.y][next.x])
//         {
//             cost[next.y][next.x] = cost[pos.y][pos.x] + 1;
//             q.push(next);
//         }
//     }
// }

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#define dlog(str) cout << "====" << str << endl;
#else
#define dlog(str)
#endif
#define INF (99999999)
#define MOD 1000000007
#define REP(i, n) for (int i = 0, i##_l = (n); i < i##_l; i++)
#define FOR(i, s, e) for (int i = s, i##_l = (e); i < i##_l; i++)
#define LLI long long int
#define _min(a, b) ((a < b) ? a : b)
#define _max(a, b) ((a < b) ? b : a)
#define chmax(a, b) a = _max(a, b)
#define chmin(a, b) a = _min(a, b)
#define bit(a, shift) ((a>>shift)&1))
#define pm(a) ((a) ? 1 : -1)
#define SORT(v) sort(v.begin(), v.end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
// int 2.14E±9    lli 9.2E±18    double 1.7E±380

template <typename T> struct vec2 {
  T x, y;
  vec2(T _x, T _y) : x(_x), y(_y) {}
};
#define veci2 vec2<int>

int d4x[4] = {1, 0, -1, 0};
int d4y[4] = {0, 1, 0, -1};

int main() {
  cout << fixed << setprecision(10);

  int h, w;
  cin >> h >> w;

  int cx, cy;
  cin >> cy >> cx;
  cx--;
  cy--;

  int dx, dy;
  cin >> dy >> dx;
  dx--;
  dy--;

  vector<string> s(h);
  REP(i, h) { cin >> s[i]; }

  vector<vector<int>> cost(h, vector<int>(w, INF));

  queue<veci2> q;
  q.push({cx, cy});
  cost[cy][cx] = 0;

  LLI mc = w * h + 1;

  while (q.empty() == false) {
    auto pos = q.front();
    q.pop();

    REP(i, 4) {
      veci2 next = pos;
      next.x += d4x[i];
      next.y += d4y[i];

      if (next.x < 0 || w <= next.x)
        continue;
      if (next.y < 0 || h <= next.y)
        continue;

      if (s[next.y][next.x] == '#')
        continue;

      if (cost[pos.y][pos.x] < cost[next.y][next.x]) {
        cost[next.y][next.x] = cost[pos.y][pos.x];
        q.push(next);
      }
    }

    FOR(x, -2, 3) {
      FOR(y, -2, 3) {
        veci2 next = pos;
        next.x += x;
        next.y += y;

        if (next.x < 0 || w <= next.x)
          continue;
        if (next.y < 0 || h <= next.y)
          continue;

        if (s[next.y][next.x] == '#')
          continue;

        if (cost[pos.y][pos.x] + 1 < cost[next.y][next.x]) {
          cost[next.y][next.x] = cost[pos.y][pos.x] + 1;
          q.push(next);
        }
      }
    }
  }
  if (cost[dy][dx] == INF) {
    cout << -1;
    return 0;
  }
  cout << cost[dy][dx];

  return 0;
}