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

// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define x first
#define y second
#define inf 1000000000000000000
#define N 1000000007
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
using namespace std;
template <class L, class R> ostream &operator<<(ostream &cout, pair<L, R> P) {
  return cout << '(' << P.x << ',' << P.y << ')';
}
template <class T> ostream &operator<<(ostream &cout, vector<T> V) {
  cout << "[ ";
  for (auto v : V)
    cout << v << ' ';
  return cout << ']';
}

vector<vector<int>> mp;
struct cmp {
  bool operator()(const pair<int, int> &p1, const pair<int, int> &p2) const {
    if (mp[p1.first][p1.second] != mp[p2.first][p2.second]) {
      return mp[p1.first][p1.second] < mp[p2.first][p2.second];
    } else {
      return p1 < p2;
    }
  }
};
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
// edge cases when n=1,m=1.wrong variable in loop.if<->while;merge
// resize;LLONG_MAX
signed main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // #endif
  speed
      // global var
      int m,
      n;
  cin >> n >> m;
  mp.resize(n, vector<int>(m, 0));
  vector<string> inp(n);
  int r, c;
  cin >> r >> c;
  --r;
  --c;
  int dr, dc;
  cin >> dr >> dc;
  --dr;
  --dc;
  for (int i = 0; i < n; ++i) {
    cin >> inp[i];
  }

  vector<vector<bool>> vis(n, vector<bool>(m, false));
  mp[r][c] = 0;

  set<pair<int, int>, cmp> st;
  st.insert({r, c});
  // cout<<"done";return 0;
  while (!st.empty()) {

    pair<int, int> curr = *st.begin();
    st.erase(st.begin());
    if (curr == make_pair(dr, dc)) {
      cout << mp[dr][dc];
      return 0;
    }
    vis[curr.first][curr.second] = true;

    for (int _ = 0; _ < 4; ++_) {
      int nx = curr.first + dx[_];
      int ny = curr.second + dy[_];
      if (nx >= 0 and ny >= 0 and nx < n and ny < m and inp[nx][ny] != '#' and
          !vis[nx][ny]) {

        if (st.find({nx, ny}) != st.end()) {

          if (mp[nx][ny] <= mp[curr.first][curr.second]) {
            continue;
          }
          st.erase({nx, ny});
        }
        mp[nx][ny] = mp[curr.first][curr.second];
        st.insert({nx, ny});
      }
    }
    for (int a = -2; a < 3; ++a) {
      for (int b = -2; b < 3; ++b) {
        int nx = curr.first + a;
        int ny = curr.second + b;
        if (nx >= 0 and ny >= 0 and nx < n and ny < m and inp[nx][ny] != '#' and
            !vis[nx][ny]) {
          if (st.find({nx, ny}) != st.end()) {
            if (mp[nx][ny] <= mp[curr.first][curr.second] + 1) {
              continue;
            }
            st.erase({nx, ny});
          }
          mp[nx][ny] = mp[curr.first][curr.second] + 1;
          st.insert({nx, ny});
        }
      }
    }
  }
  cout << -1;
}
// try thinking from a different angle after spending some time


Error: TLE

Fixed submission:

// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define x first
#define y second
#define inf 1000000000000000000
#define N 1000000007
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
using namespace std;
template <class L, class R> ostream &operator<<(ostream &cout, pair<L, R> P) {
  return cout << '(' << P.x << ',' << P.y << ')';
}
template <class T> ostream &operator<<(ostream &cout, vector<T> V) {
  cout << "[ ";
  for (auto v : V)
    cout << v << ' ';
  return cout << ']';
}

map<pair<int, int>, int> mp;
struct cmp {
  bool operator()(const pair<int, int> &p1, const pair<int, int> &p2) const {
    if (mp[p1] != mp[p2]) {
      return mp[p1] < mp[p2];
    } else {
      return p1 < p2;
    }
  }
};
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
// edge cases when n=1,m=1.wrong variable in loop.if<->while;merge
// resize;LLONG_MAX
signed main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // #endif
  speed
      // global var
      int m,
      n;
  cin >> n >> m;
  vector<string> inp(n);
  int r, c;
  cin >> r >> c;
  --r;
  --c;
  int dr, dc;
  cin >> dr >> dc;
  --dr;
  --dc;
  for (int i = 0; i < n; ++i) {
    cin >> inp[i];
  }

  map<pair<int, int>, bool> vis;
  mp[{r, c}] = 0;

  set<pair<int, int>, cmp> st;
  st.insert({r, c});
  // cout<<"done";return 0;
  while (!st.empty()) {

    pair<int, int> curr = *st.begin();
    st.erase(st.begin());
    if (curr == make_pair(dr, dc)) {
      cout << mp[{dr, dc}];
      return 0;
    }
    vis[curr] = true;

    for (int _ = 0; _ < 4; ++_) {
      int nx = curr.first + dx[_];
      int ny = curr.second + dy[_];
      if (nx >= 0 and ny >= 0 and nx < n and ny < m and inp[nx][ny] != '#' and
          !vis[{nx, ny}]) {

        if (st.find({nx, ny}) != st.end()) {

          if (mp[{nx, ny}] <= mp[curr]) {
            continue;
          }
          st.erase({nx, ny});
        }
        mp[{nx, ny}] = mp[curr];
        st.insert({nx, ny});
      }
    }
    for (int a = -2; a < 3; ++a) {
      for (int b = -2; b < 3; ++b) {
        int nx = curr.first + a;
        int ny = curr.second + b;
        if (nx >= 0 and ny >= 0 and nx < n and ny < m and inp[nx][ny] != '#' and
            !vis[{nx, ny}]) {
          if (st.find({nx, ny}) != st.end()) {
            if (mp[{nx, ny}] <= mp[curr] + 1) {
              continue;
            }
            st.erase({nx, ny});
          }
          mp[{nx, ny}] = mp[curr] + 1;
          st.insert({nx, ny});
        }
      }
    }
  }
  cout << -1;
}
// try thinking from a different angle after spending some time