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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, x, n) for (int i = x; i < (n); i++)
#define vint(a, n)                                                             \\
  vint a(n);                                                                   \\
  rep(i, n) cin >> a[i];
#define vll(a, n)                                                              \\
  vll a(n);                                                                    \\
  rep(i, n) cin >> a[i];
#define ALL(n) begin(n), end(n)
#define RALL(n) rbegin(n), rend(n)
#define MOD (1000000007)
#define INF (2e9)
#define INFL (2e18)

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
using vint = vector<int>;
using vll = vector<ll>;
using vbool = vector<bool>;
template <class T> using arr = vector<vector<T>>;
template <class T> int popcount(T &a) {
  int c = 0;
  rep(i, 8 * (int)sizeof(a)) {
    if ((a >> i) & 1)
      c++;
  }
  return c;
}
template <class T> void pr(T x) { cout << x << endl; }
template <class T> void prvec(vector<T> &a) {
  rep(i, a.size() - 1) { cout << a[i] << " "; }
  pr(a[a.size() - 1]);
}
template <class T> void prarr(arr<T> &a) {
  rep(i, a.size()) if (a[i].empty()) pr("");
  else prvec(a[i]);
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

struct dist {
  int mg, wk;

  bool operator<=(const dist &r) {
    if (this->mg != r.mg)
      return this->mg < r.mg;
    else
      return this->wk <= r.wk;
  }
};

struct P {
  int h, w;
};

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

int main() {
  int h, w, sh, sw, th, tw;
  cin >> h >> w >> sh >> sw >> th >> tw;
  sh--;
  sw--;
  th--;
  tw--;
  string s[h];
  rep(i, h) cin >> s[i];

  arr<int> g(h, vector<int>(w));
  rep(i, h) rep(j, w) { g[i][j] = INF; }

  // pr(-1); return 0;
  g[sh][sw] = 0;
  // queue<P> que;
  deque<P> que;
  que.push_back({sh, sw});

  while (!que.empty()) {
    // queue<P> next_q;
    // // if(cnt++ > h+w) break;
    // // pr(1);
    // while(!que.empty()){
    int x, y;
    x = que.front().h;
    y = que.front().w;
    que.pop_front();
    int d = g[x][y];

    rep(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (g[nx][ny] <= d)
        continue;
      g[nx][ny] = d;
      que.push_front({nx, ny});
    }

    FOR(i, -2, 3) FOR(j, -2, 3) {
      int nx = x + i, ny = y + j;
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (g[nx][ny] <= d + 1)
        continue;
      g[nx][ny] = d + 1;
      que.push_back({nx, ny});
    }
    // }
    // que = next_q;
  }

  int ans = g[th][tw];
  if (ans == INF)
    pr(-1);
  else
    pr(ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, x, n) for (int i = x; i < (n); i++)
#define vint(a, n)                                                             \\
  vint a(n);                                                                   \\
  rep(i, n) cin >> a[i];
#define vll(a, n)                                                              \\
  vll a(n);                                                                    \\
  rep(i, n) cin >> a[i];
#define ALL(n) begin(n), end(n)
#define RALL(n) rbegin(n), rend(n)
#define MOD (1000000007)
#define INF (2e9)
#define INFL (2e18)

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
using vint = vector<int>;
using vll = vector<ll>;
using vbool = vector<bool>;
template <class T> using arr = vector<vector<T>>;
template <class T> int popcount(T &a) {
  int c = 0;
  rep(i, 8 * (int)sizeof(a)) {
    if ((a >> i) & 1)
      c++;
  }
  return c;
}
template <class T> void pr(T x) { cout << x << endl; }
template <class T> void prvec(vector<T> &a) {
  rep(i, a.size() - 1) { cout << a[i] << " "; }
  pr(a[a.size() - 1]);
}
template <class T> void prarr(arr<T> &a) {
  rep(i, a.size()) if (a[i].empty()) pr("");
  else prvec(a[i]);
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

struct dist {
  int mg, wk;

  bool operator<=(const dist &r) {
    if (this->mg != r.mg)
      return this->mg < r.mg;
    else
      return this->wk <= r.wk;
  }
};

struct P {
  int h, w;
};

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

int main() {
  int h, w, sh, sw, th, tw;
  cin >> h >> w >> sh >> sw >> th >> tw;
  sh--;
  sw--;
  th--;
  tw--;
  string s[h];
  rep(i, h) cin >> s[i];

  arr<int> g(h, vector<int>(w));
  rep(i, h) rep(j, w) { g[i][j] = INF; }

  // pr(-1); return 0;
  g[sh][sw] = 0;
  queue<P> que;
  que.push({sh, sw});
  arr<bool> pushed(h, vector<bool>(w, false));

  while (!que.empty()) {
    // queue<P> next_q;
    // // if(cnt++ > h+w) break;
    // // pr(1);
    // while(!que.empty()){
    int x, y;
    x = que.front().h;
    y = que.front().w;
    que.pop();
    int d = g[x][y];
    pushed[x][y] = false;

    rep(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (g[nx][ny] <= d)
        continue;
      g[nx][ny] = d;
      que.push({nx, ny});
    }

    FOR(i, -2, 3) FOR(j, -2, 3) {
      int nx = x + i, ny = y + j;
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (g[nx][ny] <= d + 1)
        continue;
      g[nx][ny] = d + 1;
      que.push({nx, ny});
    }
    // }
    // que = next_q;
  }

  int ans = g[th][tw];
  if (ans == INF)
    pr(-1);
  else
    pr(ans);
  return 0;
}