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

#include <bits/stdc++.h>
using namespace std;
#define debug(...) 42

#define endl '\\n'

#define pb(x) push_back(x);
#define ce(x) cout << x << '\\n';

using db = long double;
using pll = pair<long long, long long>;

#define rep(i, x, n, inc) for (i = x; i < n; i += inc)

#define all(a) (a).begin(), (a).end()

#define mp(a, b) make_pair(a, b)
#define first first
#define ss second

void check() {
  if (false)
    cout << "check" << endl;
}

int main() {

  long long t, i, x, j, y, m, z = 0, k, n;
  cin >> n >> m;
  string s[n];
  check();
  long long cx, cy, dx, dy;
  check();
  cin >> cx >> cy >> dx >> dy;
  cx--;
  cy--;
  dx--;
  dy--;
  check();
  cx++;
  cy++;
  dx++;
  dy++;

  long long dynamicprogramming[n][m];
  cx--;
  cy--;
  dx--;
  dy--;
  for (long long i = 0; i < n; i++) {
    cin >> s[i];
    rep(j, 0, m, 1) dynamicprogramming[i][j] = 1000000007;
  }

  priority_queue<pair<long long, pll>> q;
  q.push(mp(0, mp(cx, cy)));

  if (s[cx][cy] != '.')
    return cout << -1, 0;

  dynamicprogramming[cx][cy] = 0;

  long long x_mo[] = {1, 0, -1, 0};
  long long y_mo[] = {0, 1, 0, -1};

  while (q.size()) {
    pair<long long, pll> temp = q.top();
    q.pop();
    x = temp.ss.first, y = temp.ss.ss;
    if (temp.first != -dynamicprogramming[x][y]) {
      continue;
    }
    for (i = 0; i < 4; i++) {
      long long xxx = x + x_mo[i];
      long long newY = y + y_mo[i];

      if (xxx < n and xxx >= 0 and newY >= 0 and newY < m) {
        if (s[xxx][newY] == '.' and
            dynamicprogramming[x][y] < dynamicprogramming[xxx][newY]) {
          dynamicprogramming[xxx][newY] =
              min(dynamicprogramming[x][y], dynamicprogramming[xxx][newY]);

          q.push(mp(-dynamicprogramming[xxx][newY], mp(xxx, newY)));
        }
      }
    }

    for (i = -2; i <= 2; i++) {
      for (j = -2; j <= 2; j++) {
        long long xxx = x + i;
        long long newY = y + j;

        if (xxx < n and xxx >= 0 and newY >= 0 and newY < m) {
          if (s[xxx][newY] == '.' and
              dynamicprogramming[x][y] + 1 < dynamicprogramming[xxx][newY]) {
            dynamicprogramming[xxx][newY] = min(dynamicprogramming[x][y] + 1,
                                                dynamicprogramming[xxx][newY]);
            q.push(mp(-dynamicprogramming[xxx][newY], mp(xxx, newY)));
          }
        }
      }
    }
  }

  z = dynamicprogramming[dx][dy];

  if (!(!(z == 1000000007))) {
    z = -1;
  };
  cout << z << "\\n";
  debug(z);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp(a, b) make_pair(a, b)
#define pll pair<ll, ll>
#define ss second
#define ff first
int main() {

  ll t, i, x, j, y, m, z = 0, k, n;
  cin >> n >> m;
  string s[n];

  ll cx, cy, dx, dy;
  cin >> cx >> cy >> dx >> dy;
  cx -= 1;
  cy -= 1;
  dx -= 1;
  dy -= 1;

  ll dp[n][m];

  for (long long i = 0; i < n; i++) {
    cin >> s[i];
    for (long long j = 0; i < m; j++)
      dp[i][j] = 1000000007;
  }

  priority_queue<pair<ll, pair<ll, ll>>> q;
  q.push(mp(0, mp(cx, cy)));

  if (s[cx][cy] != '.')
    return cout << -1, 0;

  dp[cx][cy] = 0;

  ll x_mo[] = {1, 0, -1, 0};
  ll y_mo[] = {0, 1, 0, -1};

  while (q.size()) {
    pair<ll, pll> temp = q.top();
    q.pop();
    x = temp.ss.ff, y = temp.ss.ss;
    if (temp.ff != -dp[x][y])
      continue;

    for (i = 0; i < 4; i++) {
      ll newX = x + x_mo[i];
      ll newY = y + y_mo[i];

      if (newX < n and newX >= 0 and newY >= 0 and newY < m) {
        if (s[newX][newY] == '.' and dp[x][y] < dp[newX][newY]) {
          dp[newX][newY] = min(dp[x][y], dp[newX][newY]);
          // q.push({newX, newY});
          q.push(mp(-dp[newX][newY], mp(newX, newY)));
        }
      }
    }

    for (i = -2; i <= 2; i++) {
      for (j = -2; j <= 2; j++) {
        ll newX = x + i;
        ll newY = y + j;

        if (newX < n and newX >= 0 and newY >= 0 and newY < m) {
          if (s[newX][newY] == '.' and dp[x][y] + 1 < dp[newX][newY]) {
            dp[newX][newY] = min(dp[x][y] + 1, dp[newX][newY]);
            // q.push({newX, newY});
            q.push(mp(-dp[newX][newY], mp(newX, newY)));
          }
        }
      }
    }
  }

  z = dp[dx][dy];

  if (z == 1000000007)
    z = -1;
  cout << z << "\\n";
}