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

#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
typedef long long ll;
typedef long double ld;

typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<ld> vld;

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()

int main() {
  fastIO;
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  ch--, cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--, dw--;
  vs s(h);
  vector<vi> path(h, vector<int>(w, -1));

  for (int i = 0; i < h; i++)
    cin >> s[i];
  path[ch][cw] = 0;

  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      q;
  q.push({0, {ch, cw}});

  while (!q.empty()) {
    int a = q.top().s.f;
    int b = q.top().s.s;
    int v = q.top().f;
    q.pop();

    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int x = a + i;
        int y = b + j;

        if (!i && !j || x < 0 || y < 0 || x >= h || y >= w || s[x][y] == '#')
          continue;

        if (abs(i) + abs(j) == 1 &&
            (path[x][y] == -1 || path[x][y] > path[a][b]))
          path[x][y] = path[a][b], q.push({path[x][y], {x, y}});
        else if (path[x][y] == -1 || path[x][y] > path[a][b] + 1)
          path[x][y] = path[a][b] + 1, q.push({path[x][y], {x, y}});
      }
  }

  cout << path[dh][dw] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
typedef long long ll;
typedef long double ld;

typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<ld> vld;

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()

int main() {
  fastIO;
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  ch--, cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--, dw--;
  vs s(h);
  vector<vi> path(h, vector<int>(w, -1));

  for (int i = 0; i < h; i++)
    cin >> s[i];
  path[ch][cw] = 0;

  queue<pair<int, int>> q;
  q.push({ch, cw});

  while (q.size()) {
    pair<int, int> p = q.front();
    q.pop();

    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int x = p.f + i;
        int y = p.s + j;

        if (!i && !j || x < 0 || y < 0 || x >= h || y >= w || s[x][y] == '#')
          continue;

        if (abs(i) + abs(j) == 1 &&
            (path[x][y] == -1 || path[x][y] > path[p.f][p.s]))
          path[x][y] = path[p.f][p.s], q.push({x, y});
        else if (path[x][y] == -1 || path[x][y] > path[p.f][p.s] + 1)
          path[x][y] = path[p.f][p.s] + 1, q.push({x, y});
      }
  }

  cout << path[dh][dw] << endl;

  return 0;
}