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

#include <bits/stdc++.h>
#define INF 1e9
using namespace std;
using ll = long long;
// (h, w) or (magic, dist)
using pii = pair<int, int>;

int main(void) {
  int H, W;
  cin >> H >> W;

  int Ch, Cw;
  cin >> Ch >> Cw;
  Ch--;
  Cw--;

  pii start = {Ch, Cw};

  int Dh, Dw;
  cin >> Dh >> Dw;
  Dh--;
  Dw--;

  pii goal = {Dh, Dw};

  vector<string> s(H);

  for (int i = 0; i < H; i++) {
    cin >> s[i];
  }

  vector<vector<pii>> cost(H, vector<pii>(W, {INF, INF}));
  deque<pii> que;
  cost[start.first][start.second] = {0, 0};
  que.push_back(start);

  while (!que.empty()) {
    pii current = que.front();
    que.pop_front();

    int h_s = max(0, current.first - 2);
    int h_e = min(H - 1, current.first + 2);

    int w_s = max(0, current.second - 2);
    int w_e = min(W - 1, current.second + 2);

    for (int h = h_s; h <= h_e; h++) {
      for (int w = w_s; w <= w_e; w++) {
        if (h == current.first && w == current.second)
          continue;

        if (s[h][w] == '#')
          continue;

        if ((abs(h - current.first) == 0 && abs(w - current.second) == 1) ||
            (abs(h - current.first) == 1 && abs(w - current.second) == 0)) {
          if (cost[h][w].first > cost[current.first][current.second].first) {
            cost[h][w].first = cost[current.first][current.second].first;
            cost[h][w].second = cost[current.first][current.second].second + 1;
            que.push_front({h, w});
          }
        } else {
          if (cost[h][w].first >
              cost[current.first][current.second].first + 1) {
            cost[h][w].first = cost[current.first][current.second].first + 1;
            cost[h][w].second = cost[current.first][current.second].second;
            que.push_back({h, w});
          }
        }
      }
    }
  }

  if (cost[goal.first][goal.second].first == INF) {
    cout << -1 << endl;
  } else {
    cout << cost[goal.first][goal.second].first << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 1e9
using namespace std;
using ll = long long;
// (h, w) or (magic, dist)
using pii = pair<int, int>;

int main(void) {
  int H, W;
  cin >> H >> W;

  int Ch, Cw;
  cin >> Ch >> Cw;
  Ch--;
  Cw--;

  pii start = {Ch, Cw};

  int Dh, Dw;
  cin >> Dh >> Dw;
  Dh--;
  Dw--;

  pii goal = {Dh, Dw};

  vector<string> s(H);

  for (int i = 0; i < H; i++) {
    cin >> s[i];
  }

  vector<vector<int>> cost(H, vector<int>(W, INF));
  queue<pii> que;
  cost[start.first][start.second] = 0;
  que.push(start);

  while (!que.empty()) {
    pii current = que.front();
    que.pop();

    int h_s = max(0, current.first - 2);
    int h_e = min(H - 1, current.first + 2);

    int w_s = max(0, current.second - 2);
    int w_e = min(W - 1, current.second + 2);

    for (int h = h_s; h <= h_e; h++) {
      for (int w = w_s; w <= w_e; w++) {
        if (h == current.first && w == current.second)
          continue;

        if (s[h][w] == '#')
          continue;

        if ((abs(h - current.first) == 0 && abs(w - current.second) == 1) ||
            (abs(h - current.first) == 1 && abs(w - current.second) == 0)) {
          if (cost[h][w] > cost[current.first][current.second]) {
            cost[h][w] = cost[current.first][current.second];
            que.push({h, w});
          }
        } else {
          if (cost[h][w] > cost[current.first][current.second] + 1) {
            cost[h][w] = cost[current.first][current.second] + 1;
            que.push({h, w});
          }
        }
      }
    }
  }

  if (cost[goal.first][goal.second] == INF) {
    cout << -1 << endl;
  } else {
    cout << cost[goal.first][goal.second] << endl;
  }

  return 0;
}