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

#include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
const int INF = 1e9;
typedef pair<int, pair<int, int>> P;

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

int main() {
  int H, W, CH, CW, DH, DW;
  cin >> H >> W >> CH >> CW >> DH >> DW;
  vector<string> maze(H);
  CH--;
  CW--;
  DH--;
  DW--;
  rep(i, H) cin >> maze[i];

  vector<vector<int>> res(H, vector<int>(W, INF));
  priority_queue<P, vector<P>, greater<P>> que;
  res[CH][CW] = 0;
  que.push({0, {CH, CW}});
  while (!que.empty()) {
    int d = que.top().first;
    int y = que.top().second.first, x = que.top().second.second;
    que.pop();
    if (res[y][x] < d)
      continue;
    for (int j = -2; j <= 2; j++) {
      for (int k = -2; k <= 2; k++) {
        if (j == 0 && k == 0)
          continue;
        int cx = 1;
        if (abs(j) + abs(k) == 1)
          cx = 0;
        int tx = x + j, ty = y + k;
        if (tx < 0 || tx >= W || ty < 0 || ty >= H || maze[ty][tx] == '#')
          continue;
        if (res[ty][tx] > d + cx) {
          res[ty][tx] = d + cx;
          que.push({res[ty][tx], {ty, tx}});
        }
      }
    }
  }
  int ans = res[DH][DW];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
const int INF = 1e9;
typedef pair<int, pair<int, int>> P;

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

int main() {
  int H, W, CH, CW, DH, DW;
  cin >> H >> W >> CH >> CW >> DH >> DW;
  vector<string> maze(H);
  CH--;
  CW--;
  DH--;
  DW--;
  rep(i, H) cin >> maze[i];

  vector<vector<int>> res(H, vector<int>(W, INF));
  priority_queue<P> que;
  res[CH][CW] = 0;
  que.push({0, {CH, CW}});
  while (!que.empty()) {
    int d = que.top().first;
    int y = que.top().second.first, x = que.top().second.second;
    que.pop();
    if (res[y][x] < d)
      continue;
    for (int j = -2; j <= 2; j++) {
      for (int k = -2; k <= 2; k++) {
        if (j == 0 && k == 0)
          continue;
        int cx = 1;
        if (abs(j) + abs(k) == 1)
          cx = 0;
        int tx = x + j, ty = y + k;
        if (tx < 0 || tx >= W || ty < 0 || ty >= H || maze[ty][tx] == '#')
          continue;
        if (res[ty][tx] > d + cx) {
          res[ty][tx] = d + cx;
          que.push({res[ty][tx], {ty, tx}});
        }
      }
    }
  }
  int ans = res[DH][DW];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  return 0;
}