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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef vector<int> vi;
typedef unsigned long ul;
typedef vector<ll> vl;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef set<int> si;
typedef multiset<int> mi;

const ll INF = 1e18;
const int MOD = 1e9 + 7;

int main() {

  cin.tie(NULL);
  ios_base::sync_with_stdio(false);

  int H, W;
  int cH, cW, dH, dW;

  cin >> H >> W;
  cin >> cH >> cW >> dH >> dW;
  --cH, --cW, --dH, --dW;

  vector<vector<bool>> processed(H, vector<bool>(W, false));
  vector<vector<char>> g(H, vector<char>(W));
  for (int j = 0; j < H; ++j) {
    for (int i = 0; i < W; ++i) {
      cin >> g[j][i];
    }
  }

  vector<vl> cost(H, vl(W, INF));

  cost[cH][cW] = 0;
  priority_queue<pair<int, pi>> q;
  q.push({0, {cH, cW}});

  while (!q.empty()) {

    int j, i, c;
    tie(j, i) = q.top().s;
    c = cost[j][i];
    q.pop();

    if (processed[j][i])
      continue;
    processed[j][i] = true;

    if (j - 1 >= 0 && g[j - 1][i] != '#' && cost[j - 1][i] > c)
      cost[j - 1][i] = c, q.push({-c, {j - 1, i}});
    if (j + 1 < H && g[j + 1][i] != '#' && cost[j + 1][i] > c)
      cost[j + 1][i] = c, q.push({-c, {j + 1, i}});
    if (i - 1 >= 0 && g[j][i - 1] != '#' && cost[j][i - 1] > c)
      cost[j][i - 1] = c, q.push({-c, {j, i - 1}});
    if (i + 1 < W && g[j][i + 1] != '#' && cost[j][i + 1] > c)
      cost[j][i + 1] = c, q.push({-c, {j, i + 1}});

    for (int dy = -2; dy <= 2; ++dy) {
      if (j + dy < 0 || j + dy >= H)
        continue;
      for (int dx = -2; dx <= 2; ++dx) {
        if (i + dx < 0 || i + dx >= W)
          continue;

        if (g[j + dy][i + dx] == '#')
          continue;
        if (cost[j + dy][i + dx] <= c + 1)
          continue;
        else
          cost[j + dy][i + dx] = c + 1, q.push({-(c + 1), {j + dy, i + dx}});
      }
    }

    // if(cost[dH][dW]!=INF) break;
  }

  if (cost[dH][dW] == INF)
    cout << "-1\\n";
  else
    cout << cost[dH][dW] << "\\n";

  /*for(int j=0; j<H; ++j) {
          for(int i=0; i<W; ++i) {
                  if(g[j][i]=='#') cout <<"# ";
                  else if(cost[j][i]==INF) cout << "INF ";
                  else cout << cost[j][i] << " ";
          }
          cout << "\\n";
  }*/

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef vector<int> vi;
typedef unsigned long ul;
typedef vector<ll> vl;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef set<int> si;
typedef multiset<int> mi;

const ll INF = 1e18;
const int MOD = 1e9 + 7;

int main() {

  cin.tie(NULL);
  ios_base::sync_with_stdio(false);

  int H, W;
  int cH, cW, dH, dW;

  cin >> H >> W;
  cin >> cH >> cW >> dH >> dW;
  --cH, --cW, --dH, --dW;

  vector<vector<char>> g(H, vector<char>(W));
  for (int j = 0; j < H; ++j) {
    for (int i = 0; i < W; ++i) {
      cin >> g[j][i];
    }
  }

  vector<vl> cost(H, vl(W, INF));

  cost[cH][cW] = 0;
  queue<pair<int, int>> q;
  q.push({cH, cW});

  while (!q.empty()) {

    int j, i, c;
    tie(j, i) = q.front();
    c = cost[j][i];
    q.pop();

    if (j - 1 >= 0 && g[j - 1][i] != '#' && cost[j - 1][i] > c)
      cost[j - 1][i] = c, q.push({j - 1, i});
    if (j + 1 < H && g[j + 1][i] != '#' && cost[j + 1][i] > c)
      cost[j + 1][i] = c, q.push({j + 1, i});
    if (i - 1 >= 0 && g[j][i - 1] != '#' && cost[j][i - 1] > c)
      cost[j][i - 1] = c, q.push({j, i - 1});
    if (i + 1 < W && g[j][i + 1] != '#' && cost[j][i + 1] > c)
      cost[j][i + 1] = c, q.push({j, i + 1});

    for (int dy = -2; dy <= 2; ++dy) {
      if (j + dy < 0 || j + dy >= H)
        continue;
      for (int dx = -2; dx <= 2; ++dx) {
        if (i + dx < 0 || i + dx >= W)
          continue;

        if (g[j + dy][i + dx] == '#')
          continue;
        if (cost[j + dy][i + dx] <= c + 1)
          continue;
        else
          cost[j + dy][i + dx] = c + 1, q.push({j + dy, i + dx});
      }
    }
  }

  if (cost[dH][dW] == INF)
    cout << "-1\\n";
  else
    cout << cost[dH][dW] << "\\n";

  /*for(int j=0; j<H; ++j) {
          for(int i=0; i<W; ++i) {
                  if(g[j][i]=='#') cout <<"# ";
                  else if(cost[j][i]==INF) cout << "INF ";
                  else cout << cost[j][i] << " ";
          }
          cout << "\\n";
  }*/

  return 0;
}