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

#include <bits/stdc++.h>

using namespace std;

#define INF 1000000007
#define LINF (1LL << 62)
#define PI 3.14159265358979

typedef long long i64;
typedef pair<i64, i64> P;

inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; }

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;
}

int h, w, a, b, c, d;
string s[3030];
int m[3030][3030];
void solve() {
  cin >> h >> w;
  cin >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }

  int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  int dd[5] = {-2, -1, 0, 1, 2};
  priority_queue<pair<int, P>, vector<pair<int, P>>, greater<pair<int, P>>> q;
  q.push({1, {a, b}});
  while (!q.empty()) {
    auto p = q.top();
    q.pop();
    int cnt = p.first;
    int x = p.second.first, y = p.second.second;
    for (int k = 0; k < 4; k++) {
      int nx = x + dx[k];
      int ny = y + dy[k];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (m[nx][ny] != 0 && m[nx][ny] <= cnt)
        continue;
      m[nx][ny] = cnt;
      q.push({cnt, {nx, ny}});
    }
    for (int i = 0; i < 5; i++) {
      int nx = x + dd[i];
      if (nx < 0 || nx >= h)
        continue;
      for (int j = 0; j < 5; j++) {
        int ny = y + dd[j];
        if (ny < 0 || ny >= w)
          continue;
        if (s[nx][ny] == '#')
          continue;
        if (m[nx][ny] != 0 && m[nx][ny] <= cnt + 1)
          continue;
        m[nx][ny] = cnt + 1;
        q.push({cnt + 1, {nx, ny}});
      }
    }
  }
  cout << m[c][d] - 1 << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF 1000000007
#define LINF (1LL << 62)
#define PI 3.14159265358979

typedef long long i64;
typedef pair<i64, i64> P;

inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; }

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;
}

int h, w, a, b, c, d;
string s[3030];
int m[3030][3030];
void solve() {
  cin >> h >> w;
  cin >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }

  int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  int dd[5] = {-2, -1, 0, 1, 2};
  priority_queue<pair<int, P>> q;
  q.push({1, {a, b}});
  while (!q.empty()) {
    auto p = q.top();
    q.pop();
    int cnt = p.first;
    int x = p.second.first, y = p.second.second;
    for (int k = 0; k < 4; k++) {
      int nx = x + dx[k];
      int ny = y + dy[k];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (m[nx][ny] != 0 && m[nx][ny] <= cnt)
        continue;
      m[nx][ny] = cnt;
      q.push({cnt, {nx, ny}});
    }
    for (int i = 0; i < 5; i++) {
      int nx = x + dd[i];
      if (nx < 0 || nx >= h)
        continue;
      for (int j = 0; j < 5; j++) {
        int ny = y + dd[j];
        if (ny < 0 || ny >= w)
          continue;
        if (s[nx][ny] == '#')
          continue;
        if (m[nx][ny] != 0 && m[nx][ny] <= cnt + 1)
          continue;
        m[nx][ny] = cnt + 1;
        q.push({cnt + 1, {nx, ny}});
      }
    }
  }
  cout << m[c][d] - 1 << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}