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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<long long> vl;
typedef vector<vector<ll>> vll;
typedef vector<bool> vb;
typedef set<int> si;
typedef queue<int> qi;
typedef stack<int> sti;
typedef priority_queue<int> pqi;
#define fori(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, b, a) for (int i = (b); i >= (a); --i)
#define all(a) (a).begin(), (a).end()
// #define __builtin_popcountll bitCountLL
// #define __builtin_popcount bitCount
//__builtin_ctz 末尾0的个数, __builtin_clz 前导0的个数
//__builtin_ffs 最后一个1的位置
mt19937
    mt_rand(chrono::high_resolution_clock::now().time_since_epoch().count());
// std::uniform_int_distribution<long long> rg(1,1e18);
#define maxn 100005
#define Mod 1000000007
const int INF = 2e9;
const ll ML = 4e18;

void solve() {
  int m, n;
  cin >> m >> n;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> grid(m);
  fori(i, 0, m) cin >> grid[i];

  priority_queue<vi, vii, greater<vi>> pq;
  pq.push(vi{0, x2, y2});
  vii vis(m, vi(n, 0));
  int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
  while (!pq.empty()) {
    vi tp = pq.top();
    pq.pop();
    if (vis[tp[1]][tp[2]])
      continue;
    vis[tp[1]][tp[2]] = 1;
    if (tp[1] == x1 && tp[2] == y1) {
      cout << tp[0] << endl;
      return;
    }
    fori(i, 0, 4) {
      int nx = tp[1] + dx[i], ny = tp[2] + dy[i];
      if (nx < 0 || nx >= m || ny < 0 || ny >= n || vis[nx][ny] ||
          grid[nx][ny] == '#')
        continue;
      pq.push(vi{tp[0], nx, ny});
    }
    fori(i, -2, 3) {
      fori(j, -2, 3) {
        // if(abs(i) != 2 && abs(j) != 2) continue;
        if (!i && !j)
          continue;
        int nx = tp[1] + i, ny = tp[2] + j;
        if (nx < 0 || nx >= m || ny < 0 || ny >= n || vis[nx][ny] ||
            grid[nx][ny] == '#')
          continue;
        pq.push(vi{tp[0] + 1, nx, ny});
      }
    }
  }
  cout << -1 << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // cout.precision(30); cout << fixed;
  // freopen("D:\\\\data_in.txt", "r", stdin);

  solve();

  /*
  clock_t start,finish;
  start = clock();

  finish = clock();
  cout << "\\n" << (double)(finish - start) / (double)(CLOCKS_PER_SEC) * 1000 <<
  "ms" << endl;
  */
  return 0;
}

/***************************************************************
 question:
 idea:
 tag:
 record:
 ***************************************************************/


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<long long> vl;
typedef vector<vector<ll>> vll;
typedef vector<bool> vb;
typedef set<int> si;
typedef queue<int> qi;
typedef stack<int> sti;
typedef priority_queue<int> pqi;
#define fori(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, b, a) for (int i = (b); i >= (a); --i)
#define all(a) (a).begin(), (a).end()
// #define __builtin_popcountll bitCountLL
// #define __builtin_popcount bitCount
//__builtin_ctz 末尾0的个数, __builtin_clz 前导0的个数
//__builtin_ffs 最后一个1的位置
mt19937
    mt_rand(chrono::high_resolution_clock::now().time_since_epoch().count());
// std::uniform_int_distribution<long long> rg(1,1e18);
#define maxn 100005
#define Mod 1000000007
const int INF = 2e9;
const ll ML = 4e18;

void solve() {
  int m, n;
  cin >> m >> n;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> grid(m);
  fori(i, 0, m) cin >> grid[i];
  priority_queue<vi, vii, greater<vi>> pq;
  pq.push(vi{0, x2, y2});
  vii vis(m, vi(n, 0)), check(m, vi(n, 0));
  int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
  vis[x2][y2] = 1;
  while (!pq.empty()) {
    vi tp = pq.top();
    pq.pop();
    if (check[tp[1]][tp[2]])
      continue;
    vis[tp[1]][tp[2]] = 1;
    check[tp[1]][tp[2]] = 1;
    if (tp[1] == x1 && tp[2] == y1) {
      cout << tp[0] << endl;
      return;
    }
    fori(i, 0, 4) {
      int nx = tp[1] + dx[i], ny = tp[2] + dy[i];
      if (nx < 0 || nx >= m || ny < 0 || ny >= n && vis[nx][ny] ||
          check[nx][ny] || grid[nx][ny] == '#')
        continue;
      pq.push(vi{tp[0], nx, ny});
      vis[tp[1]][tp[2]] = 1;
    }
    fori(i, -2, 3) {
      fori(j, -2, 3) {
        if (abs(i) != 2 && abs(j) != 2)
          continue;
        int nx = tp[1] + i, ny = tp[2] + j;
        if (nx < 0 || nx >= m || ny < 0 || ny >= n && vis[nx][ny] ||
            check[nx][ny] || grid[nx][ny] == '#')
          continue;
        pq.push(vi{tp[0] + 1, nx, ny});
      }
    }
  }
  cout << -1 << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // cout.precision(30); cout << fixed;
  //	freopen("D:\\\\data_in.txt", "r", stdin);

  solve();

  /*
  clock_t start,finish;
  start = clock();

  finish = clock();
  cout << "\\n" << (double)(finish - start) / (double)(CLOCKS_PER_SEC) * 1000 <<
  "ms" << endl;
  */
  return 0;
}

/***************************************************************
 question:
 idea:
 tag:
 record:
 ***************************************************************/