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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

int main() {
  ll h, w;
  cin >> h >> w;
  pair<ll, ll> start, goal;
  cin >> start.first >> start.second;
  cin >> goal.first >> goal.second;
  start.first--;
  start.second--;
  goal.first--;
  goal.second--;

  vector<string> field(h);
  for (auto &&e : field) {
    cin >> e;
  }

  vector<vector<ll>> cost(h, vector<ll>(w, -1));
  deque<pair<ll, ll>> next;
  cost[start.first][start.second] = 0;
  next.push_front(start);

  while (!next.empty()) {
    ll x = next.front().first;
    ll y = next.front().second;
    next.pop_front();

    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        if (i == 0 && j == 0)
          continue;
        ll nx = x + i, ny = y + j;
        if (nx < 0 || ny < 0 || nx >= h || ny >= w)
          continue;
        if (field[nx][ny] == '#')
          continue;
        if (abs(i) + abs(j) == 1) {
          if (cost[nx][ny] == -1) {
            cost[nx][ny] = cost[x][y];
            next.push_front({nx, ny});
          } else if (cost[nx][ny] > cost[x][y]) {
            cost[nx][ny] = cost[x][y];
            next.push_front({nx, ny});
          }
        } else {
          if (cost[nx][ny] < 0) {
            cost[nx][ny] = cost[x][y] + 1;
            next.push_back({nx, ny});
          } else if (cost[nx][ny] > cost[x][y] + 1) {
            cost[nx][ny] = cost[x][y] + 1;
            next.push_back({nx, ny});
          }
        }
      }
    }
  }

  // for (int i = 0; i < h; ++i)
  // {
  //   for (int j = 0; j < w; ++j)
  //   {
  //     cout<<cost[i][j]<<" ";
  //   }
  //   cout<<endl;
  // }

  cout << cost[goal.first][goal.second] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

const ll dx[] = {1, 0, -1, 0};
const ll dy[] = {0, 1, 0, -1};

int main() {
  ll h, w;
  cin >> h >> w;
  pair<ll, ll> start, goal;
  cin >> start.first >> start.second;
  cin >> goal.first >> goal.second;
  start.first--;
  start.second--;
  goal.first--;
  goal.second--;
  vector<string> field(h);
  for (int i = 0; i < h; ++i) {
    cin >> field[i];
  }

  vector<vector<ll>> dist(h, vector<ll>(w, -1));
  dist[start.first][start.second] = 0;
  queue<pair<ll, ll>> next;
  next.push(start);

  while (!next.empty()) {
    ll x = next.front().first;
    ll y = next.front().second;
    next.pop();

    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        if (abs(i) + abs(j) == 0)
          continue;
        ll nx = x + i;
        ll ny = y + j;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          continue;
        if (field[nx][ny] == '#')
          continue;
        if (dist[nx][ny] < 0) {
          if (abs(i) + abs(j) <= 1)
            dist[nx][ny] = dist[x][y];
          else
            dist[nx][ny] = dist[x][y] + 1;
          next.push({nx, ny});
        } else {
          if (abs(i) + abs(j) <= 1)
            dist[nx][ny] = dist[x][y];
          else
            dist[nx][ny] = dist[x][y] + 1;
          next.push({nx, ny});
        }
      }
    }
  }
  // for (int i = 0; i < h; ++i)
  // {
  //   for (int j = 0; j < w; ++j)
  //   {
  //     cout<<dist[i][j]<<" ";
  //   }
  //   cout<<endl;
  // }

  cout << dist[goal.first][goal.second] << endl;
}