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

#include <bits/stdc++.h>

#define nl '\\n'

using namespace std;

typedef long long ll;

void solve() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<vector<char>> grid(h, vector<char>(w));
  for (vector<char> &row : grid)
    for (char &cell : row)
      cin >> cell;

  vector<vector<int>> cost(h, vector<int>(w, 1e9));
  cost[ch][cw] = 0;
  deque<pair<int, int>> q;
  q.push_back({ch, cw});

  auto valid = [&, h, w, grid](int x, int y) {
    return x < h && x >= 0 && y < w && y >= 0 && grid[x][y] != '#';
  };

  while (!q.empty()) {
    auto [currX, currY] = q.front();
    q.pop_front();
    vector<pair<int, int>> next = {{currX + 1, currY},
                                   {currX - 1, currY},
                                   {currX, currY + 1},
                                   {currX, currY - 1}};

    for (auto [x, y] : next)
      if (valid(x, y) && cost[x][y] > cost[currX][currY]) {
        cost[x][y] = min(cost[x][y], cost[currX][currY]);
        q.push_front({x, y});
      }

    for (int x = currX - 2; x <= currX + 2; x++)
      for (int y = currY - 2; y <= currY + 2; y++)
        if (valid(x, y) && cost[x][y] > cost[currX][currY] + 1) {
          cost[x][y] = min(cost[x][y], cost[currX][currY] + 1);
          q.push_back({x, y});
        }
  }

  cout << (cost[dh][dw] == 1e9 ? -1 : cost[dh][dw]);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  // int t;
  // cin >> t;
  // while (t--) {
  solve();
  // }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define nl '\\n'

using namespace std;

typedef long long ll;

vector<pair<int, int>> findJumps(vector<vector<char>> &grid, int i, int j) {
  vector<pair<int, int>> coords;

  for (int x = -2; x <= 2; x++) {
    for (int y = -2; y <= 2; y++) {
      if (i + x >= grid.size() || i + x < 0 || j + y >= grid[0].size() ||
          j + y < 0)
        continue;
      if (x == 0 && y == 0)
        continue;
      if (grid[i + x][j + y] != '.')
        continue;
      coords.push_back({i + x, j + y});
    }
  }

  return coords;
}

void dfs(vector<vector<char>> &grid, int x, int y, int X, int Y, int &minCount,
         int count) {
  if (x == X && y == Y) {
    minCount = min(minCount, count);
    return;
  }
  if (count >= minCount)
    return;

  char restore = grid[x][y];
  grid[x][y] = '$';
  vector<pair<int, int>> dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

  for (pair<int, int> d : dir) {
    if (x + d.first >= grid.size() || x + d.first < 0 ||
        y + d.second >= grid[0].size() || y + d.second < 0)
      continue;
    if (grid[x + d.first][y + d.second] == '$')
      continue;
    else if (grid[x + d.first][y + d.second] == '#')
      for (pair<int, int> coords : findJumps(grid, x, y))
        dfs(grid, coords.first, coords.second, X, Y, minCount, count + 1);
    else
      dfs(grid, x + d.first, y + d.second, X, Y, minCount, count);
  }

  grid[x][y] = restore;
}

void solve() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  vector<vector<char>> grid(h, vector<char>(w));
  for (vector<char> &row : grid)
    for (char &cell : row)
      cin >> cell;

  int minCount = 1e9;
  dfs(grid, ch - 1, cw - 1, dh - 1, dw - 1, minCount, 0);

  if (minCount == 1e9)
    cout << -1;
  else
    cout << minCount;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  // int t;
  // cin >> t;
  // while (t--) {
  solve();
  // }

  return 0;
}