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

#include <bits/stdc++.h>

using namespace std;

#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define MP make_pair
#define PB push_back
#define X first
#define Y second

typedef long long int lld;
typedef pair<int, int> pii;
typedef pair<lld, lld> pll;

const int maxn = (int)1e3 + 5;
const int inf = (int)1e9 + 5;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

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

  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;

  vector<vector<int>> board(h, vector<int>(w)), dis(h, vector<int>(w, inf));

  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      board[i][j] = s[j] == '.' ? 0 : 1;
    }
  }

  priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                 greater<pair<int, pii>>>
      pq;
  pq.emplace(0, pii(ch, cw));
  dis[ch][cw] = 0;

  auto ok = [&](int x, int y) {
    return x >= 0 && x < h && y >= 0 && y < w && board[x][y] != 1;
  };

  while (!pq.empty()) {
    auto front = pq.top();
    pq.pop();

    int &x = front.second.first;
    int &y = front.second.second;
    int &d = front.first;

    // cout << x << ' ' << y << ' ' << d << '\\n';

    if (x == dh && y == dw) {
      cout << front.first << '\\n';
      return 0;
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int xx = x + i;
        int yy = y + j;

        if (!ok(xx, yy)) {
          continue;
        }

        if (abs(i) + abs(j) <= 1) {
          if (dis[xx][yy] > d) {
            dis[xx][yy] = d;
            pq.emplace(d, pii(xx, yy));
          }
        } else {
          if (dis[xx][yy] > d + 1) {
            dis[xx][yy] = d + 1;
            pq.emplace(d + 1, pii(xx, yy));
          }
        }
      }
    }
  }

  cout << -1 << '\\n';

  return 0;
}

// g++ sol.cpp -o sol.out -Wall -Wextra -Wshadow -fsanitize=undefined -std=c++17

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define MP make_pair
#define PB push_back
#define X first
#define Y second

typedef long long int lld;
typedef pair<int, int> pii;
typedef pair<lld, lld> pll;

const int maxn = (int)1e3 + 5;
const int inf = (int)1e9 + 5;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

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

  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;

  vector<vector<int>> board(h, vector<int>(w)), dis(h, vector<int>(w, inf));

  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      board[i][j] = s[j] == '.' ? 0 : 1;
    }
  }

  queue<pair<pii, int>> que;
  que.emplace(make_pair(ch, cw), 0);
  dis[ch][cw] = 0;

  auto ok = [&](int x, int y) {
    return x >= 0 && x < h && y >= 0 && y < w && board[x][y] != 1;
  };

  while (!que.empty()) {
    auto front = que.front();
    que.pop();

    int &x = front.first.first;
    int &y = front.first.second;

    //  << x << ' ' << y << '\\n';

    if (dis[x][y] < front.second) {
      continue;
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int xx = x + i;
        int yy = y + j;

        if (!ok(xx, yy)) {
          continue;
        }

        if (abs(i) + abs(j) <= 1) {
          if (dis[xx][yy] > dis[x][y]) {
            dis[xx][yy] = dis[x][y];
            que.emplace(pii(xx, yy), dis[xx][yy]);
          }
        } else {
          if (dis[xx][yy] > dis[x][y] + 1) {
            dis[xx][yy] = dis[x][y] + 1;
            que.emplace(pii(xx, yy), dis[xx][yy]);
          }
        }
      }
    }
  }

  cout << (dis[dh][dw] == inf ? -1 : dis[dh][dw]) << '\\n';

  return 0;
}

// g++ sol.cpp -o sol.out -Wall -Wextra -Wshadow -fsanitize=undefined -std=c++17