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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> ii;
typedef pair<ll, ll> pll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  map<ii, int> grid;
  for (int i = 0; i < h; i++) {
    string li;
    cin >> li;
    for (int j = 0; j < li.size(); j++) {
      if (li[j] == '.')
        grid[make_pair(i, j)] = 1e7;
    }
  }
  grid[make_pair(ch - 1, cw - 1)] = 0;
  set<ii> visited;
  vector<vector<bool>> can(h, vector<bool>(w));
  for (auto g : grid) {
    can[g.first.first][g.first.second] = true;
  }
  vector<ii> close;
  close.emplace_back(ch - 1, cw - 1);
  list<ii> far;
  while (!close.empty() || !far.empty()) {
    ii a;
    if (!close.empty()) {
      a = close.back();
      close.pop_back();
    } else {
      a = far.front();
      far.pop_front();
    }
    if (visited.count(a))
      continue;
    visited.insert(a);
    can[a.first][a.second] = false;
    ii t;
    t = make_pair(a.first - 1, a.second);
    if (t.first > -1 && t.first < h && t.second > -1 && t.second < w &&
        can[t.first][t.second] && !visited.count(t) && grid.count(t) &&
        grid[t] > grid[a]) {
      grid[t] = grid[a];
      close.push_back(t);
    }
    t = make_pair(a.first + 1, a.second);
    if (t.first > -1 && t.first < h && t.second > -1 && t.second < w &&
        can[t.first][t.second] && !visited.count(t) && grid.count(t) &&
        grid[t] > grid[a]) {
      grid[t] = grid[a];
      close.push_back(t);
    }
    t = make_pair(a.first, a.second - 1);
    if (t.first > -1 && t.first < h && t.second > -1 && t.second < w &&
        can[t.first][t.second] && !visited.count(t) && grid.count(t) &&
        grid[t] > grid[a]) {
      grid[t] = grid[a];
      close.push_back(t);
    }
    t = make_pair(a.first, a.second + 1);
    if (t.first > -1 && t.first < h && t.second > -1 && t.second < w &&
        can[t.first][t.second] && !visited.count(t) && grid.count(t) &&
        grid[t] > grid[a]) {
      grid[t] = grid[a];
      close.push_back(t);
    }
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        ii t = make_pair(a.first - i, a.second - j);
        if (t.first > -1 && t.first < h && t.second > -1 && t.second < w &&
            can[t.first][t.second] && !visited.count(t) && grid.count(t) &&
            grid[t] > grid[a] + 1) {
          grid[t] = grid[a] + 1;
          far.push_back(t);
        }
      }
    }
  }
  if (grid[make_pair(dh - 1, dw - 1)] == 1e7)
    cout << -1 << "\\n";
  else
    cout << grid[make_pair(dh - 1, dw - 1)] << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> ii;
typedef pair<ll, ll> pll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  map<ii, int> grid;
  for (int i = 0; i < h; i++) {
    string li;
    cin >> li;
    for (int j = 0; j < li.size(); j++) {
      if (li[j] == '.')
        grid[make_pair(i, j)] = 1e7;
    }
  }
  grid[make_pair(ch - 1, cw - 1)] = 0;
  set<ii> visited;
  vector<ii> close;
  close.emplace_back(ch - 1, cw - 1);
  list<ii> far;
  while (!close.empty() || !far.empty()) {
    ii a;
    if (!close.empty()) {
      a = close.back();
      close.pop_back();
    } else {
      a = far.front();
      far.pop_front();
    }
    if (visited.count(a))
      continue;
    visited.insert(a);
    ii t;
    t = make_pair(a.first - 1, a.second);
    if (!visited.count(t) && grid.count(t) && grid[t] > grid[a]) {
      grid[t] = grid[a];
      close.push_back(t);
    }
    t = make_pair(a.first + 1, a.second);
    if (!visited.count(t) && grid.count(t) && grid[t] > grid[a]) {
      grid[t] = grid[a];
      close.push_back(t);
    }
    t = make_pair(a.first, a.second - 1);
    if (!visited.count(t) && grid.count(t) && grid[t] > grid[a]) {
      grid[t] = grid[a];
      close.push_back(t);
    }
    t = make_pair(a.first, a.second + 1);
    if (!visited.count(t) && grid.count(t) && grid[t] > grid[a]) {
      grid[t] = grid[a];
      close.push_back(t);
    }
    for (int i = -2; i < 3; i++) {
      t = make_pair(a.first - 2, a.second + i);
      if (!visited.count(t) && grid.count(t) && grid[t] > grid[a] + 1) {
        grid[t] = grid[a] + 1;
        far.push_back(t);
      }
    }
    t = make_pair(a.first - 1, a.second - 2);
    if (!visited.count(t) && grid.count(t) && grid[t] > grid[a] + 1) {
      grid[t] = grid[a] + 1;
      far.push_back(t);
    }
    t = make_pair(a.first - 1, a.second - 1);
    if (!visited.count(t) && grid.count(t) && grid[t] > grid[a] + 1) {
      grid[t] = grid[a] + 1;
      far.push_back(t);
    }
    t = make_pair(a.first - 1, a.second + 1);
    if (!visited.count(t) && grid.count(t) && grid[t] > grid[a] + 1) {
      grid[t] = grid[a] + 1;
      far.push_back(t);
    }
    t = make_pair(a.first - 1, a.second + 2);
    if (!visited.count(t) && grid.count(t) && grid[t] > grid[a] + 1) {
      grid[t] = grid[a] + 1;
      far.push_back(t);
    }
    t = make_pair(a.first, a.second - 2);
    if (!visited.count(t) && grid.count(t) && grid[t] > grid[a] + 1) {
      grid[t] = grid[a] + 1;
      far.push_back(t);
    }
    t = make_pair(a.first, a.second + 2);
    if (!visited.count(t) && grid.count(t) && grid[t] > grid[a] + 1) {
      grid[t] = grid[a] + 1;
      far.push_back(t);
    }
    t = make_pair(a.first + 1, a.second - 2);
    if (!visited.count(t) && grid.count(t) && grid[t] > grid[a] + 1) {
      grid[t] = grid[a] + 1;
      far.push_back(t);
    }
    t = make_pair(a.first + 1, a.second - 1);
    if (!visited.count(t) && grid.count(t) && grid[t] > grid[a] + 1) {
      grid[t] = grid[a] + 1;
      far.push_back(t);
    }
    t = make_pair(a.first + 1, a.second + 1);
    if (!visited.count(t) && grid.count(t) && grid[t] > grid[a] + 1) {
      grid[t] = grid[a] + 1;
      far.push_back(t);
    }
    t = make_pair(a.first + 1, a.second + 2);
    if (!visited.count(t) && grid.count(t) && grid[t] > grid[a] + 1) {
      grid[t] = grid[a] + 1;
      far.push_back(t);
    }
    for (int i = -2; i < 3; i++) {
      ii t = make_pair(a.first + 2, a.second + i);
      if (!visited.count(t) && grid.count(t) && grid[t] > grid[a] + 1) {
        grid[t] = grid[a] + 1;
        far.push_back(t);
      }
    }
  }
  if (grid[make_pair(dh - 1, dw - 1)] == 1e7)
    cout << -1 << "\\n";
  else
    cout << grid[make_pair(dh - 1, dw - 1)] << "\\n";
  return 0;
}