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

#include <bits/stdc++.h>
#define FLASH ios_base::sync_with_stdio(0);
#define ll int
#define debt(x, y)                                                             \\
  cout << "#x = " << (x) << " and "                                            \\
       << "#y = " << (y) << endl;
#define deb(x) cout << "#x = " << (x) << endl;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define endl "\\n"
#define arr(a, n)                                                              \\
  for (ll i = 1; i <= n; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << "\\n";
#define vecc(a, n)                                                             \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << a[i] << " ";                                                       \\
  cout << "\\n";

using namespace std;

ll n, m;
ll dist[1010][1010];
char massiv[1010][1010];

ll used[1010][1010];

ll xx[] = {1, -1, 0, 0};
ll yy[] = {0, 0, 1, -1};

ll check(ll x, ll y) {
  if (x >= 1 && y >= 1 && x <= n && y <= m && massiv[x][y] == '.')
    return 1;
  else
    return 0;
}

priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll, ll>>>,
               greater<pair<ll, pair<ll, ll>>>>
    q;
ll startx, starty;
ll konecx, konecy;

int main() {
  FLASH;
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  cin >> startx >> starty;
  cin >> konecx >> konecy;

  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= m; j++) {
      cin >> massiv[i][j];
      dist[i][j] = INT_MAX;
    }
  }

  q.push(mp(0, mp(startx, starty)));
  dist[startx][starty] = 0;
  while (!q.empty()) {
    auto from = q.top();
    ll money = q.top().first;
    q.pop();
    if (money > dist[from.second.first][from.second.second])
      continue;
    ll sig = 0;
    for (ll i = 0; i < 4; i++) {
      ll tox = from.second.first + xx[i];
      ll toy = from.second.second + yy[i];
      if (check(tox, toy)) {
        if (dist[tox][toy] > dist[from.second.first][from.second.second]) {
          dist[tox][toy] = dist[from.second.first][from.second.second];
          q.push(mp(dist[tox][toy], mp(tox, toy)));
        }
      }
    }
    if (!sig) {
      for (ll i = -2; i <= 2; i++) {
        for (ll j = -2; j <= 2; j++) {
          ll tox = from.second.first + i;
          ll toy = from.second.second + j;
          if (check(tox, toy)) {
            if (dist[tox][toy] >
                dist[from.second.first][from.second.second] + 1) {
              dist[tox][toy] = dist[from.second.first][from.second.second] + 1;
              q.push(mp(dist[tox][toy], mp(tox, toy)));
            }
          }
        }
      }
    }
  }

  if (dist[konecx][konecy] == INT_MAX)
    cout << -1 << endl;
  else
    cout << dist[konecx][konecy] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FLASH ios_base::sync_with_stdio(0);
#define ll long long
#define debt(x, y)                                                             \\
  cout << "#x = " << (x) << " and "                                            \\
       << "#y = " << (y) << endl;
#define deb(x) cout << "#x = " << (x) << endl;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define endl "\\n"
#define arr(a, n)                                                              \\
  for (ll i = 1; i <= n; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << "\\n";
#define vecc(a, n)                                                             \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << a[i] << " ";                                                       \\
  cout << "\\n";

using namespace std;

ll n, m;
ll dist[1010][1010];
char massiv[1010][1010];

ll used[1010][1010];

ll xx[] = {1, -1, 0, 0};
ll yy[] = {0, 0, 1, -1};

ll check(ll x, ll y) {
  if (x >= 1 && y >= 1 && x <= n && y <= m && massiv[x][y] == '.')
    return 1;
  else
    return 0;
}

queue<pair<ll, ll>> q;
ll startx, starty;
ll konecx, konecy;

int main() {
  FLASH;
  cin >> n >> m;
  cin >> startx >> starty;
  cin >> konecx >> konecy;

  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= m; j++) {
      cin >> massiv[i][j];
      dist[i][j] = LLONG_MAX;
    }
  }

  q.push(mp(startx, starty));
  dist[startx][starty] = 0;
  while (!q.empty()) {
    auto from = q.front();
    q.pop();
    ll sig = 0;
    for (ll i = 0; i < 4; i++) {
      ll tox = from.first + xx[i];
      ll toy = from.second + yy[i];
      if (check(tox, toy)) {
        if (dist[tox][toy] > dist[from.first][from.second]) {
          dist[tox][toy] = dist[from.first][from.second];
          q.push(mp(tox, toy));
        }
      }
    }
    if (!sig) {
      for (ll i = -2; i <= 2; i++) {
        for (ll j = -2; j <= 2; j++) {
          ll tox = from.first + i;
          ll toy = from.second + j;
          if (check(tox, toy)) {
            if (dist[tox][toy] > dist[from.first][from.second] + 1) {
              dist[tox][toy] = dist[from.first][from.second] + 1;
              q.push(mp(tox, toy));
            }
          }
        }
      }
    }
  }

  if (dist[konecx][konecy] == LLONG_MAX)
    cout << -1 << endl;
  else
    cout << dist[konecx][konecy] << endl;

  return 0;
}