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

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mk make_pair
#define mod 1000000007
#define ll long long
#define lb lower_bound
#define ub upper_bound
#define endl "\\n"
#define ff first
#define ss second
#define prec(y, x) fixed << setprecision(y) << x
#define inf 1e18
#define pi 3.1415926535
vector<vector<int>> v, vis;
int h, w, h1, h2, w1, w2;
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> h >> w;
  cin >> h1 >> w1 >> h2 >> w2;
  map<pair<int, int>, int> mp;
  v.resize(h + 1);
  vis.resize(h + 1);
  for (int i = 0; i <= h; i++) {
    vis[i].resize(w + 1, -1);
    v[i].resize(w + 1, 0);
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      mp[mk(i, j)] = -1;
      char c;
      cin >> c;
      if (c == '.')
        v[i][j] = 1;
    }
  }
  set<pair<int, pair<int, int>>> s;
  vis[h1][w1] = 0;
  mp[mk(h1, w1)] = 0;
  s.insert(mk(0, mk(h1, w1)));
  while (s.size()) {
    auto it = s.begin();
    int a = it->ff;
    int b = it->ss.ff;
    int c = it->ss.ss;
    s.erase(it);
    for (int i = 0; i < 4; i++) {
      if (b + dx[i] >= 1 && b + dx[i] <= h && c + dy[i] >= 1 &&
          c + dy[i] <= w && v[b + dx[i]][c + dy[i]] == 1) {
        if (vis[b + dx[i]][c + dy[i]] == -1) {
          s.insert(mk(a, mk(b + dx[i], c + dy[i])));
          mp[mk(b + dx[i], c + dy[i])] = a;
          vis[b + dx[i]][c + dy[i]] = a;
        } else if ((mp[mk(b + dx[i], c + dy[i])] > a)) {
          int x = mp[mk(b + dx[i], c + dy[i])];
          auto it = s.find(mk(x, mk(b + dx[i], c + dy[i])));
          s.erase(it);
          s.insert(mk(a, mk(b + dx[i], c + dy[i])));
          mp[mk(b + dx[i], c + dy[i])] = a;
        }
      }
    }
    for (int i = b - 2; i <= b + 2; i++) {
      for (int j = c - 2; j <= c + 2; j++) {
        if (i >= 1 && i <= h && j >= 1 && j <= w && v[i][j] == 1) {
          if (vis[i][j] == -1) {
            s.insert(mk(a + 1, mk(i, j)));
            mp[mk(i, j)] = a + 1;
            vis[i][j] = a + 1;
          } else if ((mp[mk(i, j)] > a + 1)) {
            int x = mp[mk(i, j)];
            auto it = s.find(mk(x, mk(i, j)));
            s.erase(it);
            s.insert(mk(a + 1, mk(i, j)));
            mp[mk(i, j)] = a + 1;
          }
        }
      }
    }
  }
  /*for (int i = 1; i <= h; i++)
  {
          for (int j = 1; j <= w; j++)
          {
                  cout << mp[mk(i, j)] << " ";
          }
          cout << endl;
  }*/
  cout << mp[mk(h2, w2)];
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mk make_pair
#define mod 1000000007
#define ll long long
#define lb lower_bound
#define ub upper_bound
#define endl "\\n"
#define ff first
#define ss second
#define prec(y, x) fixed << setprecision(y) << x
#define pi 3.1415926535
vector<vector<int>> v, vis;
int h, w, h1, h2, w1, w2;
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int inf = 1e9;
  cin >> h >> w;
  cin >> h1 >> w1 >> h2 >> w2;
  map<pair<int, int>, int> mp;
  v.resize(h + 1);
  set<pair<int, pair<int, int>>> s;
  for (int i = 0; i <= h; i++) {
    v[i].resize(w + 1, 0);
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      mp[mk(i, j)] = 1e9;
      if (i != h1 || j != w1)
        s.insert(mk(1e9, mk(i, j)));
      char c;
      cin >> c;
      if (c == '.')
        v[i][j] = 1;
    }
  }
  // for (int i = 1; i <= h; i++)
  // {
  // 	for (int j = 1; j <= w; j++)
  // 		cout << v[i][j];
  // 	cout << endl;
  // }

  mp[mk(h1, w1)] = 0;
  s.insert(mk(0, mk(h1, w1)));
  while (s.size()) {
    auto it = s.begin();
    int a = it->ff;
    int b = it->ss.ff;
    int c = it->ss.ss;
    it = s.begin();
    s.erase(it);
    // cout << b << " " << c << " " << a << endl;
    for (int i = 0; i < 4; i++) {

      if (b + dx[i] >= 1 && b + dx[i] <= h && c + dy[i] >= 1 &&
          c + dy[i] <= w && v[b + dx[i]][c + dy[i]] == 1) {
        int x = mp[mk(b + dx[i], c + dy[i])];
        if ((x > a)) {
          auto it = s.find(mk(x, mk(b + dx[i], c + dy[i])));
          s.erase(it);
          s.insert(mk(a, mk(b + dx[i], c + dy[i])));
          mp[mk(b + dx[i], c + dy[i])] = a;
        }
      }
    }
    for (int i = b - 2; i <= b + 2; i++) {
      for (int j = c - 2; j <= c + 2; j++) {
        if (i >= 1 && i <= h && j >= 1 && j <= w && v[i][j] == 1) {
          int x = mp[mk(i, j)];
          if ((x > a + 1)) {
            auto it = s.find(mk(x, mk(i, j)));
            s.erase(it);
            s.insert(mk(a + 1, mk(i, j)));
            mp[mk(i, j)] = a + 1;
          }
        }
      }
    }
  }
  if (mp[mk(h2, w2)] == inf)
    cout << -1;
  else
    cout << mp[mk(h2, w2)];
  return 0;
}