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

#include <bits/stdc++.h>
// Fast io
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
// Class shorthands
#define ll long long
#define db long double
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
// Function Shorthands
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mp make_pair
#define po pop_back
#define eb emplace_back
// Code Shorthands
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
// Constants
#define hell 1000000007
#define PI acos(-1.0)
using namespace std;
int n, m;
int valid(int i, int j, int k, int l) {
  if (i + k < 0)
    return 0;
  if (i + k >= n)
    return 0;
  if (j + l < 0)
    return 0;
  if (j + l >= m)
    return 0;
  if (i + k == i && j + l == j)
    return 0;
  if (i + k == i && (j + l == j || j + l == j - 1 || j + l == j + 1))
    return 1;
  if (j + l == j && (i + k == i || i + k == i - 1 || i + k == i + 1))
    return 1;
  return 2;
}

void solve() {
  cin >> n >> m;
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  r1--;
  r2--;
  c1--;
  c2--;
  string s[n];
  rep(i, 0, n) cin >> s[i];
  vii adj[n * m + 1];
  rep(i, 0, n) {
    rep(j, 0, m) {
      if (s[i][j] == '.') {
        for (int k = -2; k < 3; k++) {
          for (int l = -2; l < 3; l++) {
            int o = valid(i, j, k, l);
            if (o == 2) {
              if (s[i + k][j + l] == '.') {
                adj[i * m + j].pb({(i + k) * m + (j + l), 1});
              }
            } else if (o == 1) {
              if (s[i + k][j + l] == '.') {
                adj[i * m + j].pb({(i + k) * m + (j + l), 0});
              }
            }
          }
        }
      }
    }
  }
  int dis[n * m + 1];
  memset(dis, -1, sizeof(dis));
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  q.push({0, r1 * m + c1});
  while (!q.empty()) {
    int cur = q.top().se;
    // cout<<cur<<endl;
    int d = q.top().fi;
    q.pop();
    if (dis[cur] == -1)
      dis[cur] = d;
    else
      continue;
    if (cur == r2 * m + c2)
      break;
    for (auto i : adj[cur]) {
      if (dis[i.fi] == -1) {
        q.push({d + i.second, i.fi});
      }
    }
  }
  cout << dis[r2 * m + c2] << endl;
}

int main() {
  io;
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// Fast io
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
// Class shorthands
#define ll long long
#define db long double
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
// Function Shorthands
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mp make_pair
#define po pop_back
#define eb emplace_back
// Code Shorthands
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
// Constants
#define hell 1000000007
#define PI acos(-1.0)
using namespace std;
int n, m;
int valid(int i, int j, int k, int l) {
  if (i + k < 0)
    return 0;
  if (i + k >= n)
    return 0;
  if (j + l < 0)
    return 0;
  if (j + l >= m)
    return 0;
  if (i + k == i && j + l == j)
    return 0;
  if (i + k == i && (j + l == j || j + l == j - 1 || j + l == j + 1))
    return 1;
  if (j + l == j && (i + k == i || i + k == i - 1 || i + k == i + 1))
    return 1;
  return 2;
}

void solve() {
  cin >> n >> m;
  int r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  r1--;
  r2--;
  c1--;
  c2--;
  string s[n];
  rep(i, 0, n) cin >> s[i];
  vii adj[n * m + 1];
  rep(i, 0, n) {
    rep(j, 0, m) {
      if (s[i][j] == '.') {
        for (int k = -2; k < 3; k++) {
          for (int l = -2; l < 3; l++) {
            int o = valid(i, j, k, l);
            if (o == 2) {
              if (s[i + k][j + l] == '.') {
                adj[i * m + j].pb({(i + k) * m + (j + l), 1});
              }
            } else if (o == 1) {
              if (s[i + k][j + l] == '.') {
                adj[i * m + j].pb({(i + k) * m + (j + l), 0});
              }
            }
          }
        }
      }
    }
  }
  int dis[n * m + 1];
  memset(dis, -1, sizeof(dis));
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  q.push({0, r1 * m + c1});
  while (!q.empty()) {
    int cur = q.top().se;
    int d = q.top().fi;
    q.pop();
    if (dis[cur] == -1)
      dis[cur] = d;
    for (auto i : adj[cur]) {
      if (dis[i.fi] == -1) {
        q.push({d + i.second, i.fi});
      }
    }
  }
  cout << dis[r2 * m + c2] << endl;
}

int main() {
  io;
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}