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

// For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456
// a.cpp -o a.exe in cmd
#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second

using namespace std;

int dp[3001][3001], n, m;
char ar[3001][3001];
int x1, y11, x2, y2;

inline bool chk(int x, int y, int z) {
  if (x <= 0 || y <= 0 || x > n || y > m || ar[x][y] == '#' || dp[x][y] <= z)
    return false;
  return true;
}

class comp {
public:
  bool operator()(pair<int, pii> &i, pair<int, pii> &j) { return i.f > j.f; }
};

void bfs() {
  priority_queue<pair<int, pii>, vector<pair<int, pii>>, comp> pq;
  pq.push({0, {x1, y11}});
  dp[x1][y11] = 0;
  pair<int, pii> x;
  while (!pq.empty()) {
    x = pq.top();
    pq.pop();
    pii loc = x.s;
    int i = loc.f, j = loc.s;
    if (dp[loc.f][loc.s] != x.f)
      continue;
    if (chk(i - 1, j, x.f)) {
      dp[i - 1][j] = x.f;
      pq.push({x.f, {i - 1, j}});
    }
    if (chk(i + 1, j, x.f)) {
      dp[i + 1][j] = x.f;
      pq.push({x.f, {i + 1, j}});
    }
    if (chk(i, j - 1, x.f)) {
      dp[i][j - 1] = x.f;
      pq.push({x.f, {i, j - 1}});
    }
    if (chk(i, j + 1, x.f)) {
      dp[i][j + 1] = x.f;
      pq.push({x.f, {i, j + 1}});
    }
    for (int l = i - 2; l <= i + 2; ++l) {
      for (int k = j - 2; k <= j + 2; ++k) {
        if (chk(l, k, x.f + 1)) {
          dp[l][k] = x.f + 1;
          pq.push({x.f + 1, {l, k}});
        }
      }
    }
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  int T;
  T = 1;
  // cin.ignore(); must be there when using getline(cin, s)
  while (T--) {
    int i, j;
    cin >> n >> m;
    cin >> x1 >> y11 >> x2 >> y2;
    for (i = 1; i <= n; ++i) {
      for (j = 1; j <= m; ++j) {
        cin >> ar[i][j];
        dp[i][j] = 1e9;
      }
    }
    bfs();
    if (dp[x2][y2] == 1e9)
      dp[x2][y2] = -1;
    cout << dp[x2][y2] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

// For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456
// a.cpp -o a.exe in cmd
#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second

using namespace std;

int dp[3001][3001], n, m;
char ar[3001][3001];
int x1, y11, x2, y2;

bool chk(int x, int y, int z) {
  if (x <= 0 || y <= 0 || x > n || y > m || ar[x][y] == '#' || dp[x][y] <= z)
    return false;
  return true;
}

void bfs() {
  priority_queue<pair<int, pii>> pq;
  pq.push({0, {x1, y11}});
  dp[x1][y11] = 0;
  pair<int, pii> x;
  while (!pq.empty()) {
    x = pq.top();
    pq.pop();
    pii loc = x.s;
    int i = loc.f, j = loc.s;
    // cout<<i<<"..."<<j<<endl;
    if (dp[loc.f][loc.s] != x.f)
      continue;
    if (chk(i - 1, j, x.f)) {
      dp[i - 1][j] = x.f;
      pq.push({x.f, {i - 1, j}});
    }
    if (chk(i + 1, j, x.f)) {
      dp[i + 1][j] = x.f;
      pq.push({x.f, {i + 1, j}});
    }
    if (chk(i, j - 1, x.f)) {
      dp[i][j - 1] = x.f;
      pq.push({x.f, {i, j - 1}});
    }
    if (chk(i, j + 1, x.f)) {
      dp[i][j + 1] = x.f;
      pq.push({x.f, {i, j + 1}});
    }
    for (int l = i - 2; l <= i + 2; ++l) {
      for (int k = j - 2; k <= j + 2; ++k) {
        if (chk(l, k, x.f + 1)) {
          dp[l][k] = x.f + 1;
          pq.push({x.f + 1, {l, k}});
        }
      }
    }
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  int T;
  T = 1;
  // cin.ignore(); must be there when using getline(cin, s)
  while (T--) {
    int i, j;
    cin >> n >> m;
    cin >> x1 >> y11 >> x2 >> y2;
    for (i = 1; i <= n; ++i) {
      for (j = 1; j <= m; ++j) {
        cin >> ar[i][j];
        dp[i][j] = 1e9;
      }
    }
    bfs();
    if (dp[x2][y2] == 1e9)
      dp[x2][y2] = -1;
    cout << dp[x2][y2] << endl;
  }
  return 0;
}