Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-ffloat-store")
#pragma GCC optimize("-fno-defer-pop")

typedef long long int ll;
typedef long double ld;

#define MAX 300005
#define endl endl
#define UPPER 1000000000

ll H, W;
ll C1, C2, D1, D2;
vector<string> vec;
vector<vector<ll>> flg(1000 + 1, vector<ll>(1000 + 1, 0));
vector<vector<ll>> cost(1000 + 1, vector<ll>(1000 + 1, 1e18));
set<pair<ll, pair<ll, ll>>> q;

int main() {

  std::ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> H >> W;

  cin >> C1 >> C2;

  cin >> D1 >> D2;
  C1--;
  C2--;
  D1--;
  D2--;

  for (ll i = 0; i < H; i++) {
    string s;
    cin >> s;
    vec.push_back(s);
  }

  q.insert({0, {C1, C2}});

  flg[C1][C2] = 1;

  while (q.size() > 0) {
    auto it = *q.begin();
    q.erase(it);
    flg[it.second.first][it.second.first] = 1;
    if (it.second.first == D1 && it.second.second == D2) {
      cout << it.first;
      return 0;
    }
    for (ll ii = -2; ii <= 2; ii++) {
      for (ll jj = -2; jj <= 2; jj++) {
        if (ii == 0 && jj == 0)
          continue;
        ll it1 = ii + it.second.first;
        ll it2 = jj + it.second.second;
        ll it3 = it.first;
        if (it1 < 0 || it2 < 0 || it1 >= H || it2 >= W) {
          continue;
        }
        if (vec[it1][it2] == '#') {
          continue;
        }
        if ((abs(ii) < 2 && jj == 0) || (abs(jj) < 2 && ii == 0)) {

        } else {
          it3++;
        }
        if (flg[it1][it2] == 2) {
          continue;
        }
        if (flg[it1][it2] == 0) {
          q.insert({it3, {it1, it2}});
          cost[it1][it2] = it3;
          flg[it1][it2] = 1;
        } else {
          if (cost[it1][it2] > it3) {
            // q.erase({cost[it1][it2], {it1, it2}});
            cost[it1][it2] = it3;
            q.insert({it3, {it1, it2}});
          }
        }
      }
    }
  }

  cout << -1;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-ffloat-store")
#pragma GCC optimize("-fno-defer-pop")

typedef long long int ll;
typedef long double ld;

#define MAX 300005
#define endl endl
#define UPPER 1000000000

ll H, W;
ll C1, C2, D1, D2;
vector<string> vec;

int main() {

  std::ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> H >> W;

  cin >> C1 >> C2;

  cin >> D1 >> D2;
  C1--;
  C2--;
  D1--;
  D2--;

  for (ll i = 0; i < H; i++) {
    string s;
    cin >> s;
    vec.push_back(s);
  }

  set<pair<ll, pair<ll, ll>>> q;
  q.insert({0, {C1, C2}});

  vector<vector<ll>> flg(H + 1, vector<ll>(W + 1, 0));
  vector<vector<ll>> cost(H + 1, vector<ll>(W + 1, 1e18));
  flg[C1][C2] = 1;

  while (q.size() > 0) {
    auto it = *q.begin();
    q.erase(it);
    flg[it.second.first][it.second.first] = 2;
    if (it.second.first == D1 && it.second.second == D2) {
      cout << it.first;
      return 0;
    }
    for (ll ii = -2; ii <= 2; ii++) {
      for (ll jj = -2; jj <= 2; jj++) {
        if (ii == 0 && jj == 0)
          continue;
        ll it1 = ii + it.second.first;
        ll it2 = jj + it.second.second;
        ll it3 = it.first;
        if (it1 < 0 || it2 < 0 || it1 >= H || it2 >= W) {
          continue;
        }
        if (vec[it1][it2] == '#') {
          continue;
        }
        if ((abs(ii) < 2 && jj == 0) || (abs(jj) < 2 && ii == 0)) {

        } else {
          it3++;
        }
        if (flg[it1][it2] == 2) {
          continue;
        }
        if (flg[it1][it2] == 0) {
          q.insert({it3, {it1, it2}});
          cost[it1][it2] = it3;
          flg[it1][it2] = 1;
        } else {
          if (cost[it1][it2] > it3) {
            q.erase({cost[it1][it2], {it1, it2}});
            cost[it1][it2] = it3;
            q.insert({it3, {it1, it2}});
          }
        }
      }
    }
  }

  cout << -1;
}