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

#pragma GCC optimize("O3")
// #pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx2")
#define io_init                                                                \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cout << setprecision(20)
#include <bits/stdc++.h>
constexpr int INF = 2147483647;
constexpr long long int INF_LL = 9223372036854775807;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

// Dijkstra法
struct edge {
  ll to;
  ll cost;
};
typedef pair<ll, ll> P;

void Dijkstra(int s, vector<vector<edge>> &g, vector<ll> &d) {
  priority_queue<P, vector<P>, greater<P>> q;
  d = vector<ll>(g.size(), INF_LL);
  d[s] = 0;
  q.push(P{0, s});

  while (!q.empty()) {
    P p = q.top();
    q.pop();
    ll v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < g[v].size(); i++) {
      edge e = g[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        q.push(P{d[e.to], e.to});
      }
    }
  }
}

int main() {
  int H, W;
  cin >> H >> W;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> m(H);
  for (int i = 0; i < H; i++)
    cin >> m[i];
  // auto itopos = [&](int idx) {return pair{ idx / W,idx % W }; };
  auto postoi = [&](int y, int x) { return y * W + x; };
  int neighbor[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  vector<vector<edge>> g(H * W * 2);
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      if (m[i][j] == '#')
        continue;
      for (int k = -2; k <= 2; k++)
        for (int s = -2; s <= 2; s++) {
          if (0 <= i + k && i + k < H && 0 <= j + s && j + s < W &&
              m[i + k][j + s] == '.') {
            int p = postoi(i + k, j + s), q = postoi(i, j);
            g[q].push_back({p, 1});
          }
        }
      for (int k = 0; k < 4; k++) {
        if (0 <= i + neighbor[k][0] && i + neighbor[k][0] < H &&
            0 <= j + neighbor[k][1] && j + neighbor[k][1] < W &&
            m[i + neighbor[k][0]][j + neighbor[k][1]] == '.') {
          int p = postoi(i + neighbor[k][0], j + neighbor[k][1]),
              q = postoi(i, j);
          g[q].push_back({p, 0});
        }
      }
    }
  vector<ll> dist;
  Dijkstra(postoi(ch, cw), g, dist);
  cout << (dist[postoi(dh, dw)] == INF_LL ? -1 : dist[postoi(dh, dw)]) << endl;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
// #pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx2")
#define io_init                                                                \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(0);                                                     \\
  cout << setprecision(20)
#include <bits/stdc++.h>
constexpr int INF = 2147483647;
constexpr long long int INF_LL = 9223372036854775807;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

int main() {
  int H, W;
  cin >> H >> W;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> m(H);
  for (int i = 0; i < H; i++)
    cin >> m[i];

  vector<vector<int>> used(H, vector<int>(W, -1));
  used[ch][cw] = 0;
  queue<pair<int, int>> q;
  q.push({ch, cw});
  while (!q.empty()) {
    // cout << q.size() << endl;
    auto t = q.front();
    q.pop();
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        if (0 <= t.first + i && t.first + i < H && 0 <= t.second + j &&
            t.second + j < W && m[t.first + i][t.second + j] == '.') {
          if (used[t.first + i][t.second + j] > used[t.first][t.second] + 1 ||
              used[t.first + i][t.second + j] == -1) {
            used[t.first + i][t.second + j] = used[t.first][t.second] + 1;
            q.push({t.first + i, t.second + j});
          }
        }
      }
    if (t.first != 0 && m[t.first - 1][t.second] == '.') {
      if (used[t.first - 1][t.second + 0] > used[t.first][t.second] ||
          used[t.first - 1][t.second + 0] == -1) {
        used[t.first - 1][t.second + 0] = used[t.first][t.second];
        q.push({t.first - 1, t.second + 0});
      }
    }
    if (t.first != H - 1 && m[t.first + 1][t.second] == '.') {
      if (used[t.first + 1][t.second + 0] > used[t.first][t.second] ||
          used[t.first + 1][t.second + 0] == -1) {
        used[t.first + 1][t.second + 0] = used[t.first][t.second];
        q.push({t.first + 1, t.second + 0});
      }
    }
    if (t.second != W - 1 && m[t.first][t.second + 1] == '.') {
      if (used[t.first + 0][t.second + 1] > used[t.first][t.second] ||
          used[t.first + 0][t.second + 1] == -1) {
        used[t.first + 0][t.second + 1] = used[t.first][t.second];
        q.push({t.first + 0, t.second + 1});
      }
    }
    if (t.second != 0 && m[t.first][t.second - 1] == '.') {
      if (used[t.first + 0][t.second - 1] > used[t.first][t.second] ||
          used[t.first + 0][t.second - 1] == -1) {
        used[t.first + 0][t.second - 1] = used[t.first][t.second];
        q.push({t.first + 0, t.second - 1});
      }
    }
  }
  cout << used[dh][dw] << endl;
}