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

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll h, w, inf = 1e10, sx, sy, ex, ey;
  string s;
  vector<string> maze;
  cin >> h >> w >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < h; i++) {
    cin >> s;
    maze.push_back(s);
  }

  deque<pair<pair<ll, ll>, ll>> q;

  vector<vector<ll>> dist(h, vector<ll>(w));

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      // dist[{i,j}] = inf;
      dist[i][j] = inf;
    }
  }

  // dist[{sx,sy}] = 0;
  dist[sx][sy] = 0;
  q.push_front({{sx, sy}, 0});

  while (!q.empty()) {

    pair<pair<ll, ll>, ll> entry = q.front();

    q.pop_front();
    ll cx = entry.first.first, cy = entry.first.second, ccost = entry.second;

    dist[cx][cy] = min(dist[cx][cy], ccost);
    if (cx - 1 >= 0 && maze[cx - 1][cy] == '.' && dist[cx - 1][cy] > ccost) {
      q.push_front({{cx - 1, cy}, ccost});
    }

    if (cx + 1 < h && maze[cx + 1][cy] == '.' && dist[cx + 1][cy] > ccost) {
      q.push_front({{cx + 1, cy}, ccost});
    }

    if (cy - 1 >= 0 && maze[cx][cy - 1] == '.' && dist[cx][cy - 1] > ccost) {
      q.push_front({{cx, cy - 1}, ccost});
    }

    if (cy + 1 < w && maze[cx][cy + 1] == '.' && dist[cx][cy + 1] > ccost) {
      q.push_front({{cx, cy + 1}, ccost});
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        if (cx + i >= 0 && cx + i < h && cy + j >= 0 && cy + j < w &&
            maze[cx + i][cy + j] == '.' && dist[cx + i][cy + j] > ccost + 1) {
          q.push_back({{cx + i, cy + j}, ccost + 1});
        }
      }
    }
  }

  if (dist[ex][ey] == inf) {
    cout << -1 << "\\n";
  } else {
    cout << dist[ex][ey] << "\\n";
  }
}

Error: TLE

Fixed submission:

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

int main() {
  ll h, w, inf = 1e10, sx, sy, ex, ey;
  string s;
  vector<string> maze;
  cin >> h >> w >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < h; i++) {
    cin >> s;
    maze.push_back(s);
  }

  deque<pair<pair<ll, ll>, ll>> q;

  map<pair<ll, ll>, ll> dist;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dist[{i, j}] = inf;
    }
  }

  dist[{sx, sy}] = 0;

  q.push_front({{sx, sy}, 0});

  while (!q.empty()) {

    pair<pair<ll, ll>, ll> entry = q.front();

    q.pop_front();
    ll cx = entry.first.first, cy = entry.first.second, ccost = entry.second;

    dist[{cx, cy}] = min(dist[{cx, cy}], ccost);
    if (cx - 1 >= 0 && maze[cx - 1][cy] == '.' && dist[{cx - 1, cy}] > ccost) {
      q.push_front({{cx - 1, cy}, ccost});
    }

    if (cx + 1 < h && maze[cx + 1][cy] == '.' && dist[{cx + 1, cy}] > ccost) {
      q.push_front({{cx + 1, cy}, ccost});
    }

    if (cy - 1 >= 0 && maze[cx][cy - 1] == '.' && dist[{cx, cy - 1}] > ccost) {
      q.push_front({{cx, cy - 1}, ccost});
    }

    if (cy + 1 < w && maze[cx][cy + 1] == '.' && dist[{cx, cy + 1}] > ccost) {
      q.push_front({{cx, cy + 1}, ccost});
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        if (cx + i >= 0 && cx + i < h && cy + j >= 0 && cy + j < w &&
            maze[cx + i][cy + j] == '.' && dist[{cx + i, cy + j}] > ccost + 1) {
          q.push_back({{cx + i, cy + j}, ccost + 1});
        }
      }
    }
  }

  if (dist[{ex, ey}] == inf) {
    cout << -1 << "\\n";
  } else {
    cout << dist[{ex, ey}] << "\\n";
  }
}