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

/*** akshaykumar99 ***/

#include <bits/stdc++.h>
using namespace std;

const long long MOD = 1e9 + 7;
const long double PI = acos(-1);
const long long N = 5e2 + 1;

#define ll int
// typedef long long ll;
typedef long double ld;

void test_case() {
  ll h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  string s[h];
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  if (s[ch - 1][cw - 1] == '#') {
    cout << -1 << '\\n';
    return;
  }
  ch--, cw--, dh--, dw--;
  ll dp[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      dp[i][j] = INT_MAX;
  }
  priority_queue<array<ll, 3>> q;
  q.push({0, ch, cw});
  dp[ch][cw] = 0;
  ll dx[] = {-2, -1, 0, 1, 2};
  ll dy[] = {-2, -1, 0, 1, 2};
  ll dxx[] = {-1, 1, 0, 0};
  ll dyy[] = {0, 0, -1, 1};
  while (!q.empty()) {
    int x = q.top()[1], y = q.top()[2], z = -q.top()[0];
    q.pop();
    if (dp[x][y] < z)
      continue;
    for (int i = 0; i < 4; i++) {
      if ((x + dxx[i] >= 0) && (x + dxx[i] < h) && (y + dyy[i] >= 0) &&
          (y + dyy[i] < w)) {
        if ((s[x + dxx[i]][y + dyy[i]] != '#') &&
            (dp[x + dxx[i]][y + dyy[i]] > dp[x][y])) {
          dp[x + dxx[i]][y + dyy[i]] = dp[x][y];
          q.push({-dp[x + dxx[i]][y + dyy[i]], x + dxx[i], y + dyy[i]});
        }
      }
    }
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        if ((x + dx[i] >= 0) && (x + dx[i] < h) && (y + dy[j] >= 0) &&
            (y + dy[j] < w)) {
          if ((s[x + dx[i]][y + dy[j]] != '#') &&
              (dp[x + dx[i]][y + dy[j]] > dp[x][y] + 1)) {
            dp[x + dx[i]][y + dy[j]] = dp[x][y] + 1;
            q.push({-dp[x + dx[i]][y + dy[j]], x + dx[i], y + dy[j]});
          }
        }
      }
    }
  }
  if (dp[dh][dw] == INT_MAX)
    cout << -1 << '\\n';
  else
    cout << dp[dh][dw] << '\\n';
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll tc = 1;
  // cin>>tc;
  while (tc--)
    test_case();
  return 0;
}

Error: TLE

Fixed submission:

/*** akshaykumar99 ***/

#include <bits/stdc++.h>
using namespace std;

const long long MOD = 1e9 + 7;
const long double PI = acos(-1);
const long long N = 5e2 + 1;

#define ll int
// typedef long long ll;
typedef long double ld;

void test_case() {
  ll h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  string s[h];
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  if (s[ch - 1][cw - 1] == '#') {
    cout << -1 << '\\n';
    return;
  }
  ch--, cw--, dh--, dw--;
  ll dp[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      dp[i][j] = INT_MAX;
  }
  queue<array<ll, 3>> q;
  q.push({ch, cw, 0});
  dp[ch][cw] = 0;
  ll dx[] = {-2, -1, 0, 1, 2};
  ll dy[] = {-2, -1, 0, 1, 2};
  ll dxx[] = {-1, 1, 0, 0};
  ll dyy[] = {0, 0, -1, 1};
  while (!q.empty()) {
    int x = q.front()[0], y = q.front()[1], z = q.front()[2];
    q.pop();
    if (dp[x][y] < z)
      continue;
    for (int i = 0; i < 4; i++) {
      if ((x + dxx[i] >= 0) && (x + dxx[i] < h) && (y + dyy[i] >= 0) &&
          (y + dyy[i] < w)) {
        if ((s[x + dxx[i]][y + dyy[i]] != '#') &&
            (dp[x + dxx[i]][y + dyy[i]] > dp[x][y])) {
          dp[x + dxx[i]][y + dyy[i]] = dp[x][y];
          q.push({x + dxx[i], y + dyy[i], dp[x + dxx[i]][y + dyy[i]]});
          // cout << s[x+dxx[i]][y+dyy[i]] << ' ';
          // cout << x+dxx[i] << ' ' << y+dyy[i] <<'\\n';
        }
      }
    }
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        if ((x + dx[i] >= 0) && (x + dx[i] < h) && (y + dy[j] >= 0) &&
            (y + dy[j] < w)) {
          if ((s[x + dx[i]][y + dy[j]] != '#') &&
              (dp[x + dx[i]][y + dy[j]] > dp[x][y] + 1)) {
            dp[x + dx[i]][y + dy[j]] = dp[x][y] + 1;
            q.push({x + dx[i], y + dy[j], dp[x + dx[i]][y + dy[j]]});
            // cout << dp[x+dx[i]][y+dy[j]] <<' ';
            // if(x+dx[i] == 3 && y + dy[j] == 3)
            // 	cout << dp[x][y] <<' ' << dp[x+dx[i]][y+dy[j]] << ' ';
            // cout << x+dx[i] << ' ' << y+dy[j] <<'\\n';
          }
        }
      }
    }
  }
  if (dp[dh][dw] == INT_MAX)
    cout << -1 << '\\n';
  else
    cout << dp[dh][dw] << '\\n';
  // for(int i = 0; i < h; i++)
  // {
  // 	for(int j =0; j < w; j++)
  // 		cout << dp[i][j] <<' ';
  // 	cout << '\\n';
  // }
  // cout << dh << ' ' << dw <<  ' ';
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll tc = 1;
  // cin>>tc;
  while (tc--)
    test_case();
  return 0;
}