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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
#define loop(n, i, a) for (ll i = a; i < n; i++)
#define loopR(n, i, a) for (ll i = a - 1; i >= n; i--)
#define all(arr, n) arr, arr + n
#define allv(v) (v).begin(), (v).end()
#define rallv(v) (v).rbegin(), (v).rend()
#define m_p make_pair
#define ll long long
#define pii pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pii>
#define sz(x) (int)x.size()
#define pb push_back
#define endl "\\n"
#define Endl "\\n"
#define f first
#define s second
#define mem(dp, n) memset(dp, n, sizeof dp)
int dx[] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
int KnightI[] = {2, 1, -1, -2, -2, -1, 1, 2};
int KnightJ[] = {1, 2, 2, 1, -1, -2, -2, -1};
template <typename T> void max_self(T &a, T b) { a = max(a, b); }
template <typename T> void min_self(T &a, T b) { a = min(a, b); }
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
const ll mxN = 5e5 + 10, oo = 0x3f3f3f3f, MOD = 1e9 + 7;
const long double PI = acos(-1), eps = 1.0e-14;
int h, w, ch, cw, dh, dw;
vector<string> grid(1005);
bool vis[1005][1005];
void solve() {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<vector<int>> dist(h, vector<int>(w, 1e9));
  loop(h, i, 0) cin >> grid[i];
  dist[ch][cw] = 0;
  priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                 greater<pair<int, pii>>>
      q;
  q.push({0, {ch, cw}});
  while (!q.empty()) {
    int i = q.top().s.f;
    int j = q.top().s.s;
    int cost = q.top().f;
    q.pop();
    if (vis[i][j])
      continue;
    vis[i][j] = 1;
    loop(4, k, 0) {
      int ti = i + dx[k], tj = j + dy[k];
      if (ti < 0 || tj < 0 || ti >= h || tj >= w || grid[ti][tj] == '#')
        continue;
      if (dist[ti][tj] > dist[i][j]) {
        dist[ti][tj] = dist[i][j];
        q.push({cost, {ti, tj}});
      }
    }
    loop(3, ki, -2) {
      loop(3, kj, -2) {
        int ti = ki + i, tj = j + kj;
        if (ti < 0 || tj < 0 || ti >= h || tj >= w || grid[ti][tj] == '#')
          continue;
        if (dist[ti][tj] > dist[i][j] + 1) {
          dist[ti][tj] = dist[i][j] + 1;
          q.push({cost + 1, {ti, tj}});
        }
      }
    }
  }
  int res = dist[dh][dw];
  if (res == 1e9)
    res = -1;
  cout << res << endl;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast();
  solve();
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
#define loop(n, i, a) for (ll i = a; i < n; i++)
#define loopR(n, i, a) for (ll i = a - 1; i >= n; i--)
#define all(arr, n) arr, arr + n
#define allv(v) (v).begin(), (v).end()
#define rallv(v) (v).rbegin(), (v).rend()
#define m_p make_pair
#define ll long long
#define pii pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pii>
#define sz(x) (int)x.size()
#define pb push_back
#define endl "\\n"
#define Endl "\\n"
#define f first
#define s second
#define mem(dp, n) memset(dp, n, sizeof dp)
int dx[] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
int KnightI[] = {2, 1, -1, -2, -2, -1, 1, 2};
int KnightJ[] = {1, 2, 2, 1, -1, -2, -2, -1};
template <typename T> void max_self(T &a, T b) { a = max(a, b); }
template <typename T> void min_self(T &a, T b) { a = min(a, b); }
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
const ll mxN = 5e5 + 10, oo = 0x3f3f3f3f, MOD = 1e9 + 7;
const long double PI = acos(-1), eps = 1.0e-14;
int h, w, ch, cw, dh, dw;
vector<string> grid(1005);
bool valid(int i, int j) {
  return i >= 0 && i < h && j >= 0 && j < w && grid[i][j] != '#';
}
void solve() {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<vector<int>> dist(h, vector<int>(w, 1e9));
  loop(h, i, 0) cin >> grid[i];
  dist[ch][cw] = 0;
  deque<pii> q;
  q.pb({ch, cw});
  while (!q.empty()) {
    int i = q.front().f;
    int j = q.front().s;
    q.pop_front();
    loop(4, k, 0) {
      int ti = i + dx[k], tj = j + dy[k];
      if (ti < 0 || tj < 0 || ti >= h || tj >= w || grid[ti][tj] == '#')
        continue;
      if (dist[ti][tj] > dist[i][j]) {
        dist[ti][tj] = dist[i][j];
        q.pb({ti, tj});
      }
    }
    loop(3, ki, -2) {
      loop(3, kj, -2) {
        int ti = ki + i, tj = j + kj;
        if (ti < 0 || tj < 0 || ti >= h || tj >= w || grid[ti][tj] == '#')
          continue;
        if (dist[ti][tj] > dist[i][j] + 1) {
          dist[ti][tj] = dist[i][j] + 1;
          q.pb({ti, tj});
        }
      }
    }
  }
  int res = dist[dh][dw];
  if (res == 1e9)
    res = -1;
  cout << res << endl;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast();
  solve();
}