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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define nt _int128
#define mod 1000000007
#define ppi pair<int, pair<int, int>>
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  // cin >> t;
  t = 1;
  while (t--) {
    int n, m, c1, c2, d1, d2;
    cin >> n >> m;
    cin >> c1 >> c2;
    cin >> d1 >> d2;
    int arr[n + 1][m + 1];
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= m; j++) {
        char c;
        cin >> c;
        if (c == '.')
          arr[i][j] = INT_MAX;
        else
          arr[i][j] = -1;
      }
    }
    priority_queue<ppi, vector<ppi>, greater<ppi>> q;

    q.push({0, {c1, c2}});

    while (!q.empty()) {
      ppi p = q.top();
      q.pop();
      int val = p.first;
      int x = p.second.first, y = p.second.second;
      if (arr[x][y] <= val)
        continue;
      arr[x][y] = val;
      if (x == d1 and y == d2)
        continue;
      // cout<<x<<" "<<y<<endl;
      for (int i = max(x - 2, 1); i <= min(x + 2, n); i++) {
        for (int j = max(y - 2, 1); j <= min(y + 2, m); j++) {
          if (arr[i][j] == -1 or (i == x and j == y))
            continue;
          if (i == x - 1 and j == y) {
            q.push({arr[x][y], {i, j}});
          } else if (i == x + 1 and j == y) {
            q.push({arr[x][y], {i, j}});
          } else if (i == x and j == y - 1) {
            q.push({arr[x][y], {i, j}});
          } else if (i == x and j == y + 1) {
            q.push({arr[x][y], {i, j}});
          } else {
            q.push({arr[x][y] + 1, {i, j}});
          }
        }
      }
    }
    if (arr[d1][d2] == INT_MAX)
      cout << -1;
    else
      cout << arr[d1][d2];
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define nt _int128
#define mod 1000000007
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t;
  // cin >> t;
  t = 1;
  while (t--) {
    int n, m, c1, c2, d1, d2;
    cin >> n >> m;
    cin >> c1 >> c2;
    cin >> d1 >> d2;
    int arr[n + 1][m + 1];
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= m; j++) {
        char c;
        cin >> c;
        if (c == '.')
          arr[i][j] = INT_MAX;
        else
          arr[i][j] = -1;
      }
    }
    queue<pair<int, int>> q;
    arr[c1][c2] = 0;
    q.push({c1, c2});
    while (!q.empty()) {
      pair<int, int> p = q.front();
      q.pop();
      int x = p.first, y = p.second;
      if (x == d1 and y == d2)
        continue;
      for (int i = max(x - 2, 1); i <= min(x + 2, n); i++) {
        for (int j = max(y - 2, 1); j <= min(y + 2, m); j++) {
          if (arr[i][j] == -1 or (i == x and j == y))
            continue;
          if (i == x - 1 and j == y and arr[i][j] > arr[x][y]) {
            q.push({i, j});
            arr[i][j] = arr[x][y];
          } else if (i == x + 1 and j == y and arr[i][j] > arr[x][y]) {
            q.push({i, j});
            arr[i][j] = arr[x][y];
          } else if (i == x and j == y - 1 and arr[i][j] > arr[x][y]) {
            q.push({i, j});
            arr[i][j] = arr[x][y];
          } else if (i == x and j == y + 1 and arr[i][j] > arr[x][y]) {
            q.push({i, j});
            arr[i][j] = arr[x][y];
          } else {
            if (arr[i][j] > arr[x][y] + 1) {
              q.push({i, j});
              arr[i][j] = arr[x][y] + 1;
            }
          }
        }
      }
    }
    if (arr[d1][d2] == INT_MAX)
      cout << -1;
    else
      cout << arr[d1][d2];
  }
  return 0;
}