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

#include <bits/stdc++.h>

#define gc() std::getchar()
#define pc(i) std::putchar(i)
#define enter '\\n'
#define RE cout << "Passing [" << __FUNCTION__ << "] on " << __LINE__ << enter
#define DEBUG(...)                                                             \\
  for (auto &i : __VA_ARGS__)                                                  \\
    cout << i << " ";                                                          \\
  cout << enter
using std::make_pair;

template <typename T> inline T read() {
  T x = 0;
  char ch = gc();
  bool f = 0;
  while (!std::isdigit(ch)) {
    f = (ch == '-');
    ch = gc();
  }
  while (std::isdigit(ch)) {
    x = x * 10 + (ch - '0');
    ch = gc();
  }
  return f ? -x : x;
}

template <typename T> void print(T x) {
  if (x < 0) {
    x = -x;
    pc('-');
  }
  if (x < 10) {
    pc(x + 48);
    return;
  }
  print<T>(x / 10);
  pc(x % 10 + 48);
  return;
}

namespace Solution {
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define Rep(i, j, k) for (int i = j; i >= k; --i)
#define rdi() read<int>()
#define rdl() read<long long>()
#define pti(i) print<int>(i), putchar(space)
#define ptl(i) print<long long>(i), putchar(space)
#define For(i, j, k) for (int i = j; i <= k; ++i)
using std::cin;
using std::cout;
using std::deque;
using std::endl;
using std::map;
using std::pair;
using std::queue;
using std::set;
using std::vector;
const int N = 1001, mod = 1e9 + 7;
int h, w, ch, cw, sh, sw, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0},
                          mx[N][N];
bool ok[N][N];
std::deque<pair<int, int>> Q;
void Fakemain() {
  IOS;
  cin >> h >> w >> ch >> cw >> sh >> sw;
  char c;
  For(i, 1, h) For(j, 1, w) cin >> c, ok[i][j] = c == '.';
  if (!ok[ch][cw]) {
    cout << -1;
    return;
  }
  memset(mx, 63, sizeof mx);
  mx[ch][cw] = 0;
  Q.push_back(make_pair(ch, cw));
  while (!Q.empty()) {
    auto fr = Q.front();
    Q.pop_front();
    int x = fr.first, y = fr.second;
    For(i, 0, 3) {
      int _x = x + dx[i], _y = y + dy[i];
      if (_x <= h && _x >= 1 && _y <= w && _y >= 1 && ok[_x][_y] &&
          mx[x][y] < mx[_x][_y]) {
        mx[_x][_y] = mx[x][y];
        Q.push_front(make_pair(_x, _y));
      }
    }
    For(i, -2, 2) {
      For(j, -2, 2) {
        int _x = x + i, _y = y + j;
        if (_x <= h && _x >= 1 && _y <= w && _y >= 1 && ok[_x][_y] &&
            mx[x][y] + 1 < mx[_x][_y]) {
          mx[_x][_y] = mx[x][y] + 1;
          Q.push_back(make_pair(_x, _y));
        }
      }
    }
  }
  cout << (mx[sh][sw] == 0x3f3f3f3f ? -1 : mx[sh][sw]);
  return;
}

} // namespace Solution

int main(int argc, char *argv[]) {
  Solution::Fakemain();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define gc() std::getchar()
#define pc(i) std::putchar(i)
#define enter '\\n'
#define RE cout << "Passing [" << __FUNCTION__ << "] on " << __LINE__ << enter
#define DEBUG(...)                                                             \\
  for (auto &i : __VA_ARGS__)                                                  \\
    cout << i << " ";                                                          \\
  cout << enter
using std::make_pair;

template <typename T> inline T read() {
  T x = 0;
  char ch = gc();
  bool f = 0;
  while (!std::isdigit(ch)) {
    f = (ch == '-');
    ch = gc();
  }
  while (std::isdigit(ch)) {
    x = x * 10 + (ch - '0');
    ch = gc();
  }
  return f ? -x : x;
}

template <typename T> void print(T x) {
  if (x < 0) {
    x = -x;
    pc('-');
  }
  if (x < 10) {
    pc(x + 48);
    return;
  }
  print<T>(x / 10);
  pc(x % 10 + 48);
  return;
}

namespace Solution {
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define Rep(i, j, k) for (int i = j; i >= k; --i)
#define rdi() read<int>()
#define rdl() read<long long>()
#define pti(i) print<int>(i), putchar(space)
#define ptl(i) print<long long>(i), putchar(space)
#define For(i, j, k) for (int i = j; i <= k; ++i)
using std::cin;
using std::cout;
using std::deque;
using std::endl;
using std::map;
using std::pair;
using std::queue;
using std::set;
using std::vector;
const int N = 1001, mod = 1e9 + 7;
int h, w, ch, cw, sh, sw, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0},
                          mx[N][N];
bool ok[N][N];
void Dfs(int x, int y) {
  For(i, 0, 3) {
    int _x = x + dx[i], _y = y + dy[i];
    if (_x <= h && _x >= 1 && _y <= w && _y >= 1 && ok[_x][_y] &&
        mx[x][y] < mx[_x][_y]) {
      mx[_x][_y] = mx[x][y];
      Dfs(_x, _y);
    }
  }
  For(i, -2, 2) {
    For(j, -2, 2) {
      int _x = x + i, _y = y + j;
      if (_x <= h && _x >= 1 && _y <= w && _y >= 1 && ok[_x][_y] &&
          mx[x][y] + 1 < mx[_x][_y]) {
        mx[_x][_y] = mx[x][y] + 1;
        Dfs(_x, _y);
      }
    }
  }
}
void Fakemain() {
  IOS;
  cin >> h >> w >> ch >> cw >> sh >> sw;
  char c;
  For(i, 1, h) For(j, 1, w) cin >> c, ok[i][j] = c == '.';
  if (!ok[ch][cw]) {
    cout << -1;
    return;
  }
  memset(mx, 63, sizeof mx);
  mx[ch][cw] = 0;
  Dfs(ch, cw);
  cout << (mx[sh][sw] == 0x3f3f3f3f ? -1 : mx[sh][sw]);
  return;
}

} // namespace Solution

int main(int argc, char *argv[]) {
  Solution::Fakemain();
  return 0;
}