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

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

const int INF = 1e7;
array<int, 4> dx = {-1, 0, 1, 0};
array<int, 4> dy = {0, -1, 0, 1};

int arr[1001][1001];

void solve() {
  int H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  vector<string> v(H);
  for (auto &s : v)
    cin >> s;

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      arr[i][j] = INF;
    }
  }
  arr[Ch][Cw] = 0;

  deque<pair<int, int>> Q;
  Q.push_front({Ch, Cw});

  while (!Q.empty()) {
    const pair<int, int> &p = Q.front();
    Q.pop_front();
    int first = p.first;
    int second = p.second;

    int steps = arr[first][second];
    // if (steps < cost) continue;

    // All UP, DOWN, LEFT, RIGHT
    for (int i = 0; i < (int)dx.size(); ++i) {
      int x = first + dx[i];
      int y = second + dy[i];
      if (x >= 0 && x < H && y >= 0 && y < W && v[x][y] == '.') {
        if (arr[x][y] > steps)
          Q.push_front({x, y});
        arr[x][y] = min(arr[x][y], steps);
      }
    }

    for (int i = -2; i <= 2; ++i) {
      int x = first + i;
      for (int j = -2; j <= 2 && x >= 0 && x < H; ++j) {
        int y = second + j;
        if (y < 0 || y >= W || v[x][y] == '#')
          continue;
        if (arr[x][y] > steps + 1)
          Q.push_back({x, y});
        arr[x][y] = min(steps + 1, arr[x][y]);
      }
    }
  }
  if (arr[Dh][Dw] >= INF)
    cout << -1 << "\\n";
  else
    cout << arr[Dh][Dw] << "\\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  int t = 1;
  while (t--) {
    solve();
  }
}


Error: TLE

Fixed submission:

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

const int INF = 1e7;
array<int, 4> dx = {-1, 0, 1, 0};
array<int, 4> dy = {0, -1, 0, 1};

int arr[1001][1001];

void solve() {
  int H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  vector<string> v(H);
  for (auto &s : v)
    cin >> s;

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      arr[i][j] = INF;
    }
  }
  arr[Ch][Cw] = 0;

  queue<pair<int, int>> Q;
  Q.push({Ch, Cw});

  while (!Q.empty()) {
    const pair<int, int> &p = Q.front();
    Q.pop();
    int first = p.first;
    int second = p.second;

    int steps = arr[first][second];
    // if (steps < cost) continue;

    // All UP, DOWN, LEFT, RIGHT
    for (int i = 0; i < (int)dx.size(); ++i) {
      int x = first + dx[i];
      int y = second + dy[i];
      if (x >= 0 && x < H && y >= 0 && y < W && v[x][y] == '.') {
        if (arr[x][y] > steps)
          Q.push({x, y});
        arr[x][y] = min(arr[x][y], steps);
      }
    }

    for (int i = -2; i <= 2; ++i) {
      int x = first + i;
      for (int j = -2; j <= 2 && x >= 0 && x < H; ++j) {
        int y = second + j;
        if (y < 0 || y >= W || v[x][y] == '#')
          continue;
        if (arr[x][y] > steps + 1)
          Q.push({x, y});
        arr[x][y] = min(steps + 1, arr[x][y]);
      }
    }
  }
  if (arr[Dh][Dw] >= INF)
    cout << -1 << "\\n";
  else
    cout << arr[Dh][Dw] << "\\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  int t = 1;
  while (t--) {
    solve();
  }
}