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

#include <algorithm>
#include <cassert>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;

int main() {
  ll H, W;
  ll CH, CW;
  ll DH, DW;
  scanf("%lld", &H);
  scanf("%lld", &W);
  scanf("%lld", &CH);
  scanf("%lld", &CW);
  scanf("%lld", &DH);
  scanf("%lld", &DW);
  CH--;
  CW--;
  DH--;
  DW--;

  vector<vector<bool>> mazemap(H, vector<bool>(W));
  char c;
  scanf("%c", &c);
  for (ll i = 0; i < H; i++) {
    char c;
    for (ll j = 0; j < W; j++) {
      scanf("%c", &c);
      mazemap[i][j] = (c == '.');
    }
    scanf("%c", &c);
  }

  ll ans = 127;
  if (!mazemap[CH][CW]) {
    ans = -1;
    printf("%lld\\n", ans);
    return 0;
  }

  priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll, ll>>>,
                 greater<pair<ll, pair<ll, ll>>>>
      nexts;
  nexts.push({0, {CH, CW}});
  vector<vector<ll>> depthmap(H, vector<ll>(W, __LONG_LONG_MAX__));
  while (!nexts.empty()) {
    auto current = nexts.top();
    nexts.pop();
    for (ll l = -2; l < 3; l++) {
      for (ll m = -2; m < 3; m++) {
        pair<ll, ll> currentpos = current.second;
        currentpos.first += l;
        currentpos.second += m;
        ll depth = current.first;
        if (currentpos.first < 0 || currentpos.second < 0 ||
            currentpos.first >= H || currentpos.second >= W)
          continue;

        if (!mazemap[currentpos.first][currentpos.second])
          continue;

        if (!(abs(l) <= 1 && abs(m) <= 1 && abs(l) != abs(m)))
          depth++;

        if (depthmap[currentpos.first][currentpos.second] > depth) {
          depthmap[currentpos.first][currentpos.second] = depth;
          nexts.push({depth, {currentpos.first, currentpos.second}});
        }
      }
    }
  }

  ans = depthmap[DH][DW];
  if (ans == __LONG_LONG_MAX__)
    ans = -1;
  printf("%lld\\n", ans);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;

int main() {
  ll H, W;
  ll CH, CW;
  ll DH, DW;
  scanf("%lld", &H);
  scanf("%lld", &W);
  scanf("%lld", &CH);
  scanf("%lld", &CW);
  scanf("%lld", &DH);
  scanf("%lld", &DW);
  CH--;
  CW--;
  DH--;
  DW--;

  vector<vector<bool>> mazemap(H, vector<bool>(W));
  char c;
  scanf("%c", &c);
  for (ll i = 0; i < H; i++) {
    char c;
    for (ll j = 0; j < W; j++) {
      scanf("%c", &c);
      mazemap[i][j] = (c == '.');
    }
    scanf("%c", &c);
  }

  ll ans = 127;
  if (!mazemap[CH][CW]) {
    ans = -1;
    printf("%lld\\n", ans);
    return 0;
  }

  queue<tuple<ll, ll, ll>> nexts;
  nexts.push({0, CH, CW});
  vector<vector<ll>> depthmap(H, vector<ll>(W, __LONG_LONG_MAX__));
  while (!nexts.empty()) {
    auto current = nexts.front();
    nexts.pop();
    for (ll l = -2; l < 3; l++) {
      if (get<1>(current) + l < 0)
        continue;

      if (get<1>(current) + l >= H)
        break;

      for (ll m = -2; m < 3; m++) {
        if (get<2>(current) + m < 0)
          continue;

        if (get<2>(current) + m >= W)
          break;

        if (l == 0 && m == 0)
          continue;

        pair<ll, ll> currentpos = {get<1>(current) + l, get<2>(current) + m};
        ll depth = get<0>(current);
        if (!mazemap[currentpos.first][currentpos.second])
          continue;

        if (!(abs(l) <= 1 && abs(m) <= 1 && abs(l) != abs(m)))
          depth++;

        if (depthmap[currentpos.first][currentpos.second] > depth) {
          depthmap[currentpos.first][currentpos.second] = depth;
          nexts.push(make_tuple(depth, currentpos.first, currentpos.second));
        }
      }
    }
  }

  ans = depthmap[DH][DW];
  if (ans == __LONG_LONG_MAX__)
    ans = -1;
  printf("%lld\\n", ans);
}