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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, b, a) for (int i = b; i >= a; i--)
#define fori(a) for (auto i : a)
#define all(a) begin(a), end(a)
#define set(a, b) memset(a, b, sizeof(a))
#define sz(a) a.size()
// #define pi 3.14159
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front // deque
#define mp make_pair
#define pq priority_queue
#define mod 1000000007
#define f first
#define s second
#define pi pair<int, int>
#define pii pair<int, pi>
#define vi vector<int>
#define vpii vector<pii>
#define debug(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";
#define tc                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

using namespace std;
using namespace std::chrono;

string repeat(string s, int n) {
  string s1 = "";
  for (int i = 0; i < n; i++)
    s1 += s;
  return s1;
}
string getString(char x) {
  string s(1, x);
  return s;
}

void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
vector<vector<int>> vis;
int h, w, ch, cw, dh, dw;
int X[] = {1, 0, 0, -1};
int Y[] = {0, 1, -1, 0};
bool safe(int x, int y) { return x >= 0 && x < h && y >= 0 && y < w; }
void solve() {
  // auto start = high_resolution_clock::now();
  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--, cw--, dh--, dw--;
  char a[h][w];
  rep(i, 0, h) {
    string s;
    cin >> s;
    rep(j, 0, w) a[i][j] = s[j];
  }
  vis = vector<vector<int>>(h, vector<int>(w, 0));
  // dfs(ch,cw);
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  vector<vector<int>> dp(h, vector<int>(w, INT_MAX));
  pq.push({0, {ch, cw}});
  while (!pq.empty()) {
    pii x = pq.top();
    pq.pop();
    if (x.s.f == dh && x.s.s == dw) {
      cout << x.f << endl;
      return;
    }
    rep(i, 0, 4) {
      int x1 = X[i] + x.s.f, y1 = Y[i] + x.s.s;

      if (safe(x1, y1) && dp[x1][y1] > x.f && a[x1][y1] == '.') {
        dp[x1][y1] = x.f;
        pq.push({x.f, {x1, y1}});
      }
    }

    rep(i, -2, 3) {
      rep(j, -2, 3) {
        if (safe(x.s.f + i, x.s.s + j) && !(i == 0 && j == 0) &&
            dp[x.s.f + i][x.s.s + j] > x.f + 1 &&
            a[x.s.f + i][x.s.s + j] == '.') {
          dp[x.s.f + i][x.s.s + j] = x.f + 1;
          pq.push({x.f + 1, {x.s.f + i, x.s.s + j}});
        }
      }
    }
  }
  cout << -1 << endl;
}
int main() {
  optimizeIO();
  // tc
  { solve(); }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, b, a) for (int i = b; i >= a; i--)
#define fori(a) for (auto i : a)
#define all(a) begin(a), end(a)
#define set(a, b) memset(a, b, sizeof(a))
#define sz(a) a.size()
// #define pi 3.14159
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front // deque
#define mp make_pair
#define pq priority_queue
#define mod 1000000007
#define f first
#define s second
#define pi pair<int, int>
#define pii pair<int, pi>
#define vi vector<int>
#define vpii vector<pii>
#define debug(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";
#define tc                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

using namespace std;
using namespace std::chrono;

string repeat(string s, int n) {
  string s1 = "";
  for (int i = 0; i < n; i++)
    s1 += s;
  return s1;
}
string getString(char x) {
  string s(1, x);
  return s;
}

void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
vector<vector<int>> vis;
int h, w, ch, cw, dh, dw;
int X[] = {1, 0, 0, -1};
int Y[] = {0, 1, -1, 0};
bool safe(int x, int y) { return x >= 0 && x < h && y >= 0 && y < w; }
void solve() {
  auto start = high_resolution_clock::now();
  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--, cw--, dh--, dw--;
  char a[h][w];
  rep(i, 0, h) {
    string s;
    cin >> s;
    rep(j, 0, w) a[i][j] = s[j];
  }
  vis = vector<vector<int>>(h, vector<int>(w, 0));
  // dfs(ch,cw);
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  vector<vector<int>> dp(h, vector<int>(w, INT_MAX));
  pq.push({0, {ch, cw}});
  while (!pq.empty()) {
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop - start);
    if (duration.count() > 1000000) {
      cout << -1 << endl;
    }
    pii x = pq.top();
    pq.pop();
    // cout<<x.s.f<<" "<<x.s.s<<endl;
    if (x.s.f == dh && x.s.s == dw) {
      cout << x.f << endl;
      return;
    }
    rep(i, 0, 4) {
      int x1 = X[i] + x.s.f, y1 = Y[i] + x.s.s;

      if (safe(x1, y1) && dp[x1][y1] > x.f && a[x1][y1] == '.') {
        dp[x1][y1] = x.f;
        pq.push({x.f, {x1, y1}});
      }
    }

    rep(i, -2, 3) {
      rep(j, -2, 3) {
        if (safe(x.s.f + i, x.s.s + j) && !(i == 0 && j == 0) &&
            dp[x.s.f + i][x.s.s + j] > x.f + 1 &&
            a[x.s.f + i][x.s.s + j] == '.') {
          pq.push({x.f + 1, {x.s.f + i, x.s.s + j}});
        }
      }
    }
  }
  cout << -1 << endl;
}
int main() {
  optimizeIO();
  // tc
  { solve(); }
}