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

/*
    Auther: ghoshashis545 Ashis Ghosh
    College: Jalpaiguri Govt Enggineering College
*/
#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll int
#define int long long
#define ld long double
#define ff first
#define ss string
#define se second
#define endl "\\n"
#define alt(v) v.begin(), v.end()
#define ralt(v) v.rbegin(), v.rend()
#define pb emplace_back
#define mp make_pair
#define fab(i, a, b) for (int i = (a); i < (b); i++)
#define fba(i, a, b) for (int i = (b); i >= (a); i--)
int mod = 1000000007;
// int mod=998244353;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};

bool test = 0;
const int N = 1e3 + 5;
const int inf = 1e18;
int n, m, x1, x2, d1, d2;
void solve() {

  cin >> n >> m;
  cin >> x1 >> x2;
  cin >> d1 >> d2;

  x1--, x2--, d1--, d2--;

  vector<string> maze(n);
  for (int i = 0; i < n; ++i)
    cin >> maze[i];

  int vis[n][m];
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      vis[i][j] = inf;

  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> q;
  q.push({x1, x2});
  vis[x1][x2] = 0;

  while (!q.empty()) {
    int x = q.top().ff;
    int y = q.top().se;
    q.pop();
    for (int i = -2; i <= 2; ++i)
      for (int j = -2; j <= 2; ++j) {
        int p1 = x + i;
        int p2 = y + j;
        if (p1 < 0 or p2 < 0 or p1 >= n or p2 >= m or maze[p1][p2] == '#')
          continue;
        int f = (abs(i) + abs(j) > 1);
        if (vis[p1][p2] > vis[x][y] + f) {
          vis[p1][p2] = vis[x][y] + f;
          q.push({p1, p2});
        }
      }
  }

  if (vis[d1][d2] == inf)
    vis[d1][d2] = -1;

  cout << vis[d1][d2];
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  if (test)
    cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll long long
ll r, c;
ll vis[3005][3005], dis[3005][3005];
char A[3005][3005];
queue<pair<ll, ll>> q;
void go(int i, int j) {
  q.push(make_pair(i, j)); // put them in queue
  vis[i][j] = 1;           // this is visited
  dis[i][j] = 0;           // initially no magic is needed
  while (!q.empty()) {
    ll u = q.front().first; // nearest point
    ll v = q.front().second;
    q.pop();
    for (int x = -2; x <= 2; x++) // try 5x5 grid
    {
      for (int y = -2; y <= 2; y++) {
        if (u + x > r || u + x < 1 || v + y > c || v + y < 1 ||
            (x == 0 && y == 0)) // out of bound
          continue;
        if (vis[u + x][v + y] == 0 && A[u + x][v + y] == '.') // possible to go
        {
          vis[u + x][v + y] = 1;
          if (abs(x) + abs(y) > 1) // if its a magic, OPeration B
            dis[u + x][v + y] = dis[u][v] + 1;
          else // operation A
            dis[u + x][v + y] = dis[u][v];
          q.push(make_pair(u + x, v + y));
        } else if (A[u + x][v + y] == '.') // to find minimum dis
        {
          if ((abs(x) + abs(y) > 1) &&
              dis[u + x][v + y] > dis[u][v] + 1) // can be improved
          { // +1 because we are using magic
            dis[u + x][v + y] = dis[u][v] + 1;
            q.push(make_pair(u + x, v + y));
          } else if (dis[u][v] < dis[u + x][v + y] &&
                     (abs(x) + abs(y) <= 1)) // can be improved
          {
            dis[u + x][v + y] = dis[u][v];
            q.push(make_pair(u + x, v + y));
          }
        }
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll sx, sy, dx, dy;
  string s;
  cin >> r >> c;
  cin >> sx >> sy;
  cin >> dx >> dy;
  for (int i = 1; i <= r; i++) {
    cin >> s;
    for (int j = 1; j <= c; j++) {
      A[i][j] = s[j - 1];
      dis[i][j] = 99999999999999; // fill dis with inf
    }
  }
  go(sx, sy);           // start from source
  if (vis[dx][dy] == 0) // not possible
  {
    cout << -1 << endl;
  } else {
    cout << dis[dx][dy] << endl; // minimum distance
  }
  return 0;
}