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

#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;
#define _USE_MATH_DEFINES
#define M_PI 3.14159265358979323846
#define MOD 1000000007
#define ll long long int
#define pii pair<ll, ll>
#define pb push_back
#define MIN -9, 223, 372, 036, 854, 775
#define N 100005
#define all(v) v.begin(), v.end()

void solve() {
  ll i, j, k, n, ans = -1, m, x, y, dx, dy;
  cin >> n >> m >> x >> y >> dx >> dy;
  char g[n][m];
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
      cin >> g[i][j];
  x--;
  y--;
  dx--;
  dy--;

  priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<pair<ll, pii>>>
      pq;
  pq.push({0, {x, y}});
  int vis[n][m];
  memset(vis, 0, sizeof(vis));

  ll r[] = {1, -1, 0, 0};
  ll c[] = {0, 0, 1, -1};

  while (!pq.empty()) {
    ll d = pq.top().first;
    ll x = pq.top().second.first;
    ll y = pq.top().second.second;
    pq.pop();
    if (x < 0 || y < 0 || x >= n || y >= m)
      continue;
    if (vis[x][y])
      continue;
    // cout<<x<<" "<<y<<"\\n";
    if (x == dx && y == dy) {
      ans = d;
      break;
    }
    vis[x][y] = 1;

    for (i = 0; i < 4; i++) {
      if (x + r[i] < n && y + c[i] < m && x + r[i] >= 0 && y + c[i] >= 0) {
        if (g[x + r[i]][y + c[i]] == '.')
          pq.push({d, {x + r[i], y + c[i]}});
      }
    }
    for (j = -2; j < 3; j++) {
      for (k = -2; k < 3; k++) {
        ll tx = x + j;
        ll ty = y + k;
        if (tx < n && ty < m && tx >= 0 && ty >= 0) {
          if (g[tx][ty] == '.')
            pq.push({d + 1, {tx, ty}});
        }
      }
    }
  }

  cout << ans;
}

int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
    cout << "\\n";
  }
  return 0;
}
/*

4 5
1 2
2 5
#.###
####.4 5
1 2
2 5
#.###
####.
#..##
#..##

#..##
#..##

*/


Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;
#define _USE_MATH_DEFINES
#define M_PI 3.14159265358979323846
#define MOD 1000000007
#define ll long long int
#define pii pair<ll, ll>
#define pb push_back
#define MIN -9, 223, 372, 036, 854, 775
#define N 100005
#define all(v) v.begin(), v.end()

void solve() {
  ll i, j, k, n, ans = -1, m, x, y, dx, dy;
  cin >> n >> m >> x >> y >> dx >> dy;
  char g[n][m];
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
      cin >> g[i][j];
  x--;
  y--;
  dx--;
  dy--;

  priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<pair<ll, pii>>>
      pq;
  pq.push({0, {x, y}});
  int vis[n][m];
  memset(vis, 0, sizeof(vis));

  ll r[] = {1, -1, 0, 0};
  ll c[] = {0, 0, 1, -1};

  ll R[] = {2, -2, 0, 0};
  ll C[] = {0, 0, 2, -2};

  while (!pq.empty()) {
    ll d = pq.top().first;
    ll x = pq.top().second.first;
    ll y = pq.top().second.second;
    pq.pop();
    if (x < 0 || y < 0 || x >= n || y >= m)
      continue;
    if (vis[x][y])
      continue;
    // cout<<x<<" "<<y<<"\\n";
    if (x == dx && y == dy) {
      ans = d;
      break;
    }
    vis[x][y] = 1;

    for (i = 0; i < 4; i++) {
      if (x + r[i] < n && y + c[i] < m && x + r[i] >= 0 && y + c[i] >= 0) {
        if (g[x + r[i]][y + c[i]] == '.')
          pq.push({d, {x + r[i], y + c[i]}});
        else {
          for (j = -2; j < 3; j++) {
            for (k = -2; k < 3; k++) {
              ll tx = x + j;
              ll ty = y + k;

              if (tx < n && ty < m && tx >= 0 && ty >= 0) {
                if (g[tx][ty] == '.')
                  pq.push({d + 1, {tx, ty}});
              }
            }
          }
        }
      }
    }
  }
  cout << ans;
}

int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
    cout << "\\n";
  }
  return 0;
}
/*

4 5
1 2
2 5
#.###
####.
#..##
#..##

*/