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

// #pragma GCC optimize("trapv")
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
#define int long long
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()

using namespace std;

const int MOD = 1e9 + 7;
const int INF = 1e9 * 1e9 + 10;

void fast() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

const int sz = 1e3 + 200;
char g[sz][sz];
bool used[sz][sz];
int dist[sz][sz];
vector<pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

signed main() {
  fast();
  int h, w;
  cin >> h >> w;

  pair<int, int> st = {0, 0}, nd = {0, 0};
  cin >> st.first >> st.second;
  cin >> nd.first >> nd.second;
  --st.first;
  --st.second;
  --nd.first;
  --nd.second;

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> g[i][j];
      dist[i][j] = INF;
    }
  }

  dist[st.first][st.second] = 0;
  deque<pair<int, int>> q;
  q.push_back(st);

  while (q.size() > 0) {
    pair<int, int> v = q.front();
    q.pop_front();

    for (auto cur : dir) {
      if (v.first + cur.first < h && v.second + cur.second >= 0 &&
          v.second + cur.second < w &&
          dist[v.first + cur.first][v.second + cur.second] >
              dist[v.first][v.second] &&
          v.first + cur.first >= 0 &&
          g[v.first + cur.first][v.second + cur.second] == '.') {
        used[v.first + cur.first][v.second + cur.second] = 1;
        dist[v.first + cur.first][v.second + cur.second] =
            dist[v.first][v.second];
        q.push_front(make_pair(v.first + cur.first, v.second + cur.second));
      }
    }

    for (int j = -2; j < 3; ++j) {
      for (int i = -2; i < 3; ++i) {
        pair<int, int> cur = {j, i};
        if (v.first + cur.first < h && v.first + cur.first >= 0 &&
            v.second + cur.second >= 0 && v.second + cur.second < w &&
            dist[v.first + cur.first][v.second + cur.second] >
                dist[v.first][v.second] + 1 &&
            g[v.first + cur.first][v.second + cur.second] == '.') {
          used[v.first + cur.first][v.second + cur.second] = 1;
          dist[v.first + cur.first][v.second + cur.second] =
              dist[v.first][v.second] + 1;
          q.push_back(make_pair(v.first + cur.first, v.second + cur.second));
        }
      }
    }
  }

  if (dist[nd.first][nd.second] == INF) {
    cout << "-1\\n";
  } else {
    cout << dist[nd.first][nd.second] << '\\n';
  }
  return 0;
}


Error: 0

Fixed submission:

// #pragma GCC optimize("trapv")
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
#define int long long
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()

using namespace std;

const int MOD = 1e9 + 7;
const int INF = 1e9 * 1e9 + 10;

void fast() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

const int sz = 1e3 + 200;
char g[sz][sz];
bool used[sz][sz];
int dist[sz][sz];
vector<pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

signed main() {
  fast();
  int h, w;
  cin >> h >> w;

  pair<int, int> st, nd;
  cin >> st.first >> st.second;
  cin >> nd.first >> nd.second;
  --st.first;
  --st.second;
  --nd.first;
  --nd.second;

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> g[i][j];
      dist[i][j] = INF;
    }
  }

  dist[st.first][st.second] = 0;
  deque<pair<int, int>> q;
  q.push_back(st);

  while (q.size() > 0) {
    pair<int, int> v = q.front();
    q.pop_front();

    for (auto cur : dir) {
      if (v.first + cur.first < h && v.second + cur.second >= 0 &&
          v.second + cur.second < w &&
          dist[v.first + cur.first][v.second + cur.second] >
              dist[v.first][v.second] &&
          v.first + cur.first >= 0 &&
          g[v.first + cur.first][v.second + cur.second] == '.') {
        used[v.first + cur.first][v.second + cur.second] = 1;
        dist[v.first + cur.first][v.second + cur.second] =
            dist[v.first][v.second];
        q.push_front(make_pair(v.first + cur.first, v.second + cur.second));
      }
    }

    for (int j = -2; j < 3; ++j) {
      for (int i = -2; i < 3; ++i) {
        pair<int, int> cur = {j, i};
        if (v.first + cur.first < h && v.first + cur.first >= 0 &&
            v.second + cur.second >= 0 && v.second + cur.second < w &&
            dist[v.first + cur.first][v.second + cur.second] >
                dist[v.first][v.second] + 1 &&
            g[v.first + cur.first][v.second + cur.second] == '.') {
          used[v.first + cur.first][v.second + cur.second] = 1;
          dist[v.first + cur.first][v.second + cur.second] =
              dist[v.first][v.second] + 1;
          q.push_back(make_pair(v.first + cur.first, v.second + cur.second));
        }
      }
    }
  }

  if (dist[nd.first][nd.second] == INF) {
    cout << "-1\\n";
  } else {
    cout << dist[nd.first][nd.second] << '\\n';
  }
  return 0;
}