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

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

// template
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;
const double eps = 1e-12;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// end

typedef pair<int, int> P;
typedef pair<int, P> T;

char g[1010][1010];
int dst[1010][1010];
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};

int main() {
  int h, w;
  cin >> h >> w;
  int x, y, gx, gy;
  cin >> x >> y >> gx >> gy;
  x--;
  y--;
  gx--;
  gy--;
  rep(i, 0, h) rep(j, 0, w) {
    cin >> g[i][j];
    dst[i][j] = inf;
  }
  dst[x][y] = 0;
  priority_queue<T, vector<T>, greater<T>> que;
  que.push({0, {x, y}});
  while (!que.empty()) {
    auto [d, pos] = que.top();
    que.pop();
    if (dst[pos.first][pos.second] < d)
      continue;
    rep(i, 0, 4) {
      int tx = pos.first + dx[i], ty = pos.second + dy[i];
      if (tx < 0 or tx > h or ty < 0 or ty > w or g[tx][ty] == '#')
        continue;
      if (chmin(dst[tx][ty], d))
        que.push({d, {tx, ty}});
    }
    rep(i, -2, 3) rep(j, -2, 3) {
      int tx = pos.first + i, ty = pos.second + j;
      if (tx < 0 or tx > h or ty < 0 or ty > w or g[tx][ty] == '#')
        continue;
      if (chmin(dst[tx][ty], d + 1))
        que.push({d + 1, {tx, ty}});
    }
  }
  if (dst[gx][gy] == inf)
    cout << "-1" << endl;
  else
    cout << dst[gx][gy] << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

// template
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;
const double eps = 1e-12;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// end

typedef pair<int, int> P;
typedef pair<int, P> T;

char g[1010][1010];
int dst[1010][1010];
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};

int main() {
  int h, w;
  cin >> h >> w;
  int x, y, gx, gy;
  cin >> x >> y >> gx >> gy;
  x--;
  y--;
  gx--;
  gy--;
  rep(i, 0, h) rep(j, 0, w) {
    cin >> g[i][j];
    dst[i][j] = inf;
  }
  dst[x][y] = 0;
  queue<T> que;
  que.push({0, {x, y}});
  while (!que.empty()) {
    auto [d, pos] = que.front();
    que.pop();
    if (dst[pos.first][pos.second] < d)
      continue;
    rep(i, 0, 4) {
      int tx = pos.first + dx[i], ty = pos.second + dy[i];
      if (tx < 0 or tx > h or ty < 0 or ty > w or g[tx][ty] == '#')
        continue;
      if (chmin(dst[tx][ty], d))
        que.push({d, {tx, ty}});
    }
    rep(i, -2, 3) rep(j, -2, 3) {
      int tx = pos.first + i, ty = pos.second + j;
      if (tx < 0 or tx > h or ty < 0 or ty > w or g[tx][ty] == '#')
        continue;
      if (chmin(dst[tx][ty], d + 1))
        que.push({d + 1, {tx, ty}});
    }
  }

  rep(i, 0, h) {
    rep(j, 0, w) cerr << dst[i][j] << ' ';
    cerr << '\\n';
  }

  if (dst[gx][gy] == inf)
    cout << "-1" << endl;
  else
    cout << dst[gx][gy] << endl;
  return 0;
}